I have a Rails 3 partial that lists all categories as a navigation menu – it’s on most, but not all of my template pages…let’s say about 75%. I’m trying to test the partial (in RSpec) right now, and I’ve just realised a few things:
At the moment, I’m calling Categories.all in the actual view. The difficulty is that, because that touches the database, my mocks/stubs in the view spec are ignored, and consequently the test fails.
I’m guessing the alternative is to assign the variable in the application controller, and then pass it as a local variable to the partial. Still, about 25% of my pages won’t use the variable and I’m wondering if there’s a more graceful way of doing things.
In short, I want view specs to pass without touching the test DB, but I’m not sure a global variable passed to my partial is the best way to do it…and I’m not declaring the variable in every (& only) those controllers who require it.
Any suggestions appreciated…
Why not create a helper method for all categories?
Or…
You can then stub out this method in your specs and you won’t be touching the DB