Rails 3 and Rspec: counter cache column being updated to 2 when expected 1

不羁的心 提交于 2019-12-05 12:43:47

I had the same problem. It turned out that my spec_helper.rb was loading the models a second time and therefore creating a second callback to update the counters. Make sure your Solution model isn't being reloaded by another process.

The answer above is also correct: you need to use == instead of be to do the comparison, but that will not fix the multiple updates that you are seeing in your log file.

You've the answer in your logs:

  • When you use be, it compares the object_id which is always the same for a couple of objects like true and 1. The id of 1 appears to be 2. Try in console: 1.object_id #=> 2

  • So replace your test with: solution.reload.likes_count.should eql 1 or even solution.reload.likes_count.should == 1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!