acts_as_votable icon indicating copy to clipboard operation
acts_as_votable copied to clipboard

cached_votes_total doesn't update during factory bot tests

Open Guy--L opened this issue 2 years ago • 0 comments

Using factory bot to create vote and votable in tests does not give the expected '2 votes' result from this view:

        <%= item.cached_votes_total > 0 ? pluralize(item.cached_votes_total, 'vote') : '' %>

Here is the factory for item and vote

FactoryBot.define do
  factory :item do
  ...
  end

  trait :carrying_votes do
    after(:create) do |item|
      create_list(:vote, 2, votable: item)
    end
  end
end
FactoryBot.define do
    factory :vote, class: ActsAsVotable::Vote do
        association :votable, factory: :item
        association :voter, factory: :user
    end
end

The test itself fails if the comment hash is removed...

    it 'lists items with votes' do
      item = create(:item, :carrying_votes)

      get root_path

      expect(response.body).to include(item.voter_names)
      # expect(response.body).to include('2 votes')
    end

What am I missing?

Thank you.

Guy--L avatar Jan 03 '23 14:01 Guy--L