Reputation: 9246
I'm having a problem with counter_cache
. Say I have three models. User and Article have many ArticleUpvotes. User can create ArticleUpvote for an article.
# models/user.rb
class User < ActiveRecord::Base
has_many :upvotes, class_name: 'ArticleUpvote'
def upvote(article)
article.upvotes.create(user: self)
end
end
# models/article.rb
class Article < ActiveRecord::Base
has_many :upvotes, class_name: 'ArticleUpvote'
end
# models/article_upvote.rb
class ArticleUpvote < ActiveRecord::Base
include ArticleVote
belongs_to :article, dependent: :destroy, counter_cache: :upvotes_count
end
# models/concerns/article_vote
module ArticleVote
extend ActiveSupport::Concern
included do
belongs_to :user
validates :user, presence: true
end
end
context 'voting' do
let(:user) { FactoryGirl.create(:user) }
let(:article) { FactoryGirl.create(:article) }
context '#upvote' do
it 'adds upvote to article' do
user.upvote(article)
expect(article.upvotes.size).to eq 1
end
end
end
1) User voting #upvote adds upvote to article
Failure/Error: expect(article.upvotes.size).to eq 1
expected: 1
got: 0
(compared using ==)
Just changing my test body to:
user.upvote(article)
article.upvotes.size # Added this line compared to failing version
expect(article.upvotes.size).to eq 1
Or doing this:
expect{ user.upvote(article) }.to change{ article.upvotes.size }.by 1
Makes tests pass. Why is this happening?
Upvotes: 0
Views: 308
Reputation: 53038
Change your example as below:
it 'adds upvote to article' do
user.upvote(article)
expect(article.reload.upvotes.size).to eq 1
end
The reason why your given example failed was that the spec was holding the article
object which was created via FactoryGirl
using FactoryGirl.create(:article)
and it does not know that there was a change made in the database. You will need to reload
the article so that the change gets reflected.
In other passing test,i.e.,
expect{ user.upvote(article) }.to change{ article.upvotes.size }.by 1
your example passes because an implicit reload happens because of change
method.
Upvotes: 1