RSpec Anti-Pattern - Don't Set @controller yourself

Posted by Rick DeNatale Wed, 26 Mar 2008 21:30:00 GMT

I was adding to an existing controller today at work and I was stumped because one of the examples in its controller spec was failing due to an exception thrown while rendering a view.

Normally RSpec controller tests, which are usually used to specify controller logic, bypass view rendering unless you specifically use the integrate_views method in the example group. The idea is that you should use view specs to specify the logic and output of views.

Since I was working in existing code, which pre-existed my being here, I’d followed some of the existing example groups in the file. These would typically look something like this:

describe HobbitController, "doing something" do

    before(:each) do
      @controller = HobbitController.new
      #.. more setup stubbing etc.
    end

   #examples
end

I finally realized that that first line in the before block was clobbering RSpecs ability to control whether or not views should be rendered. RSpec actually creates the controller instance variable for you, and extends it with a module which overrides the render method to allow integrate_views to control the rendering while allowing the expect_render expectation to work whether or not rendering actually happens.

So I’ve got a new task to clean up all the other controller specs.

Posted in  | Tags  | no comments | no trackbacks

Ch-ch-changes

Posted by Rick DeNatale Sat, 22 Mar 2008 20:45:00 GMT

Things have been hectic over the past month or so, and big changes have been in the wind.

As of the 3rd of March, I’ve been working full-time at Near Time as a Senior developer.

I really enjoyed the projects I did with Terralien and I can’t thank Nathaniel enough, but the opportunity to have a steady diet of work, and to work on a large-scale project with a talented group which is enthusiastic about my favorite tools and technologies like Ruby, Rails, and RSpec was just too good to pass up.

I must admit that I’m still adjusting to the idea of a 9-5 job (actually more like 9-7 or 8), and a 30+ mile commute instead of waking up the stairs, or out to the family room, but I’m having a blast.

I still plan to post my thoughts here, although perhaps a bit less frequently, albeit more frequently than over the past month.

Posted in  | no comments | no trackbacks