I have updated a rails app I have been working on recently to a more recent version of rails 3.2, and all my tests where failing. Finally managed to have that working, figured I’d show how.
Mocking inherited_resources helpers in views specs.
I know I shouldn’t be using inherited_resources
anymore (see here and here) but I want to release my app before I change everything to use responders.
So, my tests where failing because I was using the resource
, collection
and resource_class
helpers from some views I was using. So first my tests are failing because resource_class
isn’t available in my views. I would have thought that the controller helpers were available in the views, but they aren’t.
The solution is easy. Let’s add the following to our spec/support
directory :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
And see how to transform our old (failing) test :
1 2 3 4 5 6 7 8 9 10 11 12 |
|
becomes :
1 2 3 4 5 6 7 8 9 10 |
|
Using shared inherited partial in our views specs
Rails 3.1+ offers views inheritance, so I changed my code to have the following :
1 2 3 4 5 6 7 8 9 10 11 |
|
Then I just created a base/new.html.haml
and a base/edit.html.haml
views, to use the views inheritance.
1 2 |
|
1 2 |
|
And I have two _form.html.haml
partials, one for each controller.
Now the next issue is that our edit and new views are shared, but we still want to test the _form.html.haml
partial.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Shared partial
Finally, when testing for example cars/index.html.haml
which uses a partial toolbar.html.haml
that actually exists in base
views, the following lines are necessary :
1 2 3 |
|
This was raised as an issue to the rails team, but they commented (rightly I think) that the inheritance is related to the controller, not the views, so the test case shouldn’t know about it and you’ll have to declare it manually using the lines above.
Now let’s go back and make these tests green again.