The problem is the results printed to the screen by capybara/rspec are different to the values in the database.
Background - I have a many-to-many relationship between promo and products. - Promo has a products_counter column to keep track of the number of products related to a promo. This is handled with counter_cache on the association. - This functionality is working. I have tested it through the front end - basic crud functionality - and all the data is correct on the front end.
Something is happening and I can't figure it out. My spec fails because the page does not have the value '1' on it which is the product count. The page instead has 2 for the product count on the page. I thought that this issue could be a point in the right direction, but I had no luck with that and there isn't much else on google.
See spec below.
require 'rails_helper'
include Warden::Test::Helpers
Warden.test_mode!
feature 'Promo index page' do
scenario 'displays a list of promos' do
admin = create(:user, :admin)
p1 = create(:promo)
p1.products << create(:product)
login_as(admin)
visit promos_path
puts p1.products.count # => 1 in console
puts p1.products_count # => 1 in console
# save_and_open_page => this gives me a view into the page
# on the browser - lets me see that product count is printing
# to the screen as 2
expect(page).to have_text p.code
expect(page).to have_text p.name
expect(page).to have_text p.products.count # => fails because page displays 2 instead of 1
end
end
I have included the view for those that want to see how it's being rendered.
header.full-width-row
.columns.large-12
= link_to 'New Promo', new_promo_path, class: 'right button tiny radius'
section.full-width-row
.columns.large-12
table.full-width
thead
tr
td = "Code"
td = "Name"
td = "Product Count"
tbody
- @promos.each do |p|
tr
td.filter-on = p.code
td.filter-on = link_to p.name, promo_path(p)
td = p.products_count
Any help appreciated.