My controller RSpec tests were running great! The user selected items from a checkbox list, clicked a button and sent a PUT to the controller method. A record was created and the user was redirected to the next page. Done!
Then I had to go ahead and add logic in the controller method to make sure the user actually selected something.
Time for a screenshot to set the stage. This is a list of stores. When a user fails to select a Store the new logic displays a flash message and reloads the Store Selection Page.
Here’s the controller logic with the new logic that makes sure a store was selected. Without the params[:store_ids].nil? test we perform the necessary updates and move on to the next step. The problem is that our test can’t get past the first line.
RSpec is saying, “DUDE! WHERE’S OUR PARAMS[:STORE_IDS]???” and redirects us back to the store list.
Giving RSpec What It Wants
In previous (successful) tests we passed the user_id parameter to satisfying our routing requirements and we were golden. Now RSpec apparently wants a params[:store_ids].
But hey, we know our app’s routes better than RSpec does and we know we’re passing a :user_id parameter and a :store_id parameter ALL THE TIME. That’s what our routes always look for, so let’s add a :store_id value!
That oughta do it.
Let’s look again at that controller code (for context, it was circled in an earlier screenshot above).
if params[:store_ids].nil? …
:STORE_IDS???? I don’t like it, but it seems to be what RSpec is asking for.
THE TESTS, THEY PASS!
The Moral to Our Story
Tonight’s story might be boring as heck, but it’s intended to demonstrate the importance of RSpec PUT and GET actions corresponding with app method logic and not preconceived notions of satisfying a route.
This final pic may highlight the main point of this post better than any screenshots shown up to this point. RSpec is looking for method requirements, not exclusively route requirements. In the completed test we’re going to reload the Store List page if our store_ids parameter is missing. We do that by simply not passing a :store_ids parameter value. Both of these tests pass, and we are moving on.