Ever seen something like this in a Capybara view spec?
Capybara simulates user behavior, it will act like if a user is visiting the page and interact with it. For acceptance testing this is just fine. It will call the controller and render the page, full stack, and it's probably what you want.
Sometimes, however, you do not want to test the full stack. Sometimes you want to test in isolation, only testing your view without running the controller code.
It has to be said this is not the goal of Capybara. Capybara is a tool made for acceptance testing, not for unit testing. So, why do it? Using Webrat is a simple workaround but I don't like to mix both frameworks in the same project so here is a hack on how you can use Capybara to test views in isolation:
It reads the HAML file and renders its HTML. Don't forget to specify your view variables as they won't exists since we are not using the controller.
Since we don't use the Capybara 'page' element we don't have access to all the nice has_xpath? and has_css? selectors so we'll need to add these ourselves. You can easily do this by extending the String class, just add the following in a spec/support/string.rb file:
Make sure you have the Nokogiri gem installed and voila, that should be it.
This post is open source. Did you spot a mistake? Ideas for improvements? Contribute to this post via Github. Thank you!