I have this code in spec/models:
describe "update ranking position and player's points" do
before do
@player.save
@player2 = Player.create(firstname: "Second", lastname: "Player", nickname: "second99")
@match = Match.create(
{ loserscore: 1, winner_player_id: @player.id,
loser_player_id: @player2.id })
@player.update_rank
@player2.update_rank
Player.update_position
end
it { expect(@player.position).to eq 1 }
end
This is the method from model:
def self.update_position
@players = Player.all
@players = @players.sort_by { |player| player.rank.to_i }.reverse
@players.each_with_index do |player, index|
player.position = index + 1
player.save
end
end
When I call this method in controller everything is OK (ranking is updated).
But in RSpec I get nil
1) Player update ranking position and player's points should eq 1
Failure/Error: it { expect(@player.position).to eq 1 }
expected: 1
got: nil
I tried debug it so I wrote in RSpec:
p Player.all.count
but it shows
2
So for me everything looks OK - there are two rows in Player table.
Every others tests passed. I have the problem only with this one method (this is only one class method in this model). App works fine.
Why called method doesn't change position column in Player table?
@player.reloadafterPlayer.update_position?@player.reload?