RSpec Testing for Filtered Selections

Here’s our scenario.  We have a list of stores and a list of coupons by store. We’re going to use checkboxes on our list of stores and display only those store coupons.  Our RSpec test is going to confirm that none of the non-checked store coupons are displayed. Our Store checklist.

The coupons list that we want to make sure contains only the coupons for the selected stores, in this case, Kmart.

Our formatted rspec tests for the store-coupon lists at the page request level looks like this.

We’re going to use FactoryGirl to generate 3 stores and a coupon for each store. The checkboxes signify the store’s “active” state,  so for the 3rd store-coupon pair we set active to false. Only active store coupons should display.  We’re creating a UserStore data object for each store because Store has_many coupons through UserStore, where the active flag is set.

The page should display the first two store-coupon pairs, but not the 3rd.  We’re using Capybara’s have_selector
matcher for the test.