A problem when testing a CallBack in model

hey guys:
I met a very strange problem when testing a CallBack in model.

I think the codes are very straightforward.

model:

class Download < ActiveRecord::Base
belongs_to :user
belongs_to :drink
after_create :add_download_count

def add_download_count
drink = Drink.find(self.drink_id)
drink.download_count += 1
drink.save
end
end

test:

require ‘test_helper’

class DownloadTest < ActiveSupport::TestCase

Replace this with your real tests.

def setup
@drink = drinks(:one)
@user = users(:quentin)
end

test “the download count should increase when some one download a
drink” do

old_download_count = @drink.download_count
puts @drink.download_count
d = Download.new(:drink_id => @drink.id, :user_id => @user.id, 

:stars => 5)
assert d.save
assert_equal old_download_count + 1, @drink.reload.download_count

end
end

But the test is always because the last assert.
the @drink’s download_count is not increase…

<6> expected but was
<5>.

This problem really make me crazy…
Hope you guys can give me some help

thanks a lot…

Terry

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs