RSpec and Rails Partials
- 13th May , 2008 by Sam in RSpec, Ruby on Rails
- 0 comments
For some reason Test:Unit and I are not friends. I’m not really sure why, but I always drop to voice mail when I call. So, I moved and started working on project with Andy and Heather Tinkham (two local MPLS software testers) to learn RSpec. More to come on that at a later date. Stealth mode now. Move along, nothing to see…
It’s been fun and I’m enjoying BDD. Once I get the basics down I’ll get into RSpec stories too. Anyway, back to the point.
On my current project we are working on a new version of the most important feature of the site. Without it, everything else is crap. It’s the main driver for adding content and it needs to be rock solid, no, diamond solid. And being a newly minted RSpec user I wanted spec it all out, cover every angle, even add view specs. Here is where my noobness shines.
The new feature is in it’s own partial which is nested in another partial nested within a page. Pretty standard stuff. With the process for writing Rails views and partials in mind I set about trying to write nested view specs. That doesn’t work. AT ALL. The RSpec view docs talk about the expect_render and stub_render methods but I still couldn’t figure it out.
So I called google and still nothing really popped out until I saw an archived email chain where someone said just spec the partials. WTF? OK? So I added a new file to my specs: spec/views/my_view_folder/partial_name.html.erb_spec.rb, added the following, and waited for autotest to run. And you know what? That bastard ran. Maybe I’m just dense, or missed a paragraph somewhere, but an example of the right way to do partial specs would have been sweet. I hope this helps other unsuspecting noob.
Note: Just in case this genero code is confusing replace “my_view_folder” with the actual name of the view directory and “partial” with the actual partial name. And change ControllerHelper to be the name of the actual helper. You get that when you run the RSpec scaffold generator. I did that because I’m lazy and didn’t want to make all the spec files by hand.
UPDATE: I’ve updated the source to show how to do locals.
Resources I found useful

