Skip to content

cached_votes_total doesn't update during factory bot tests #223

@Guy--L

Description

@Guy--L

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions