# A problem when testing a CallBack in model

**URL:** <https://www.ruby-forum.com/t/a-problem-when-testing-a-callback-in-model/182293>\
**Category:** Rails\
**Created:** [January 26, 2010, 7:00am UTC](https://www.ruby-forum.com/t/a-problem-when-testing-a-callback-in-model/182293 "2010-01-26T07:00:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![terrytie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@terrytie](https://www.ruby-forum.com/u/terrytie)\
**Post date:** [January 26, 2010, 7:00am UTC](https://www.ruby-forum.com/t/a-problem-when-testing-a-callback-in-model/182293/1 "2010-01-26T07:00:53Z")

</div>

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
