rails 2-2-stable/master-HEAD view spec render expectation problem/fix…

I recently upgraded my office project to use rails 2-2-stable branch. Apart from very minor/easily fixable things rspec worked fine for me(i had to upgrade to rspec and rspec-rails though… i checked-out tag 1.1.9 and got it to rewrite everything but spec/spec_helper.rb[ruby script/generate rspec])…

The only major problem i faced with new rails and rspec marriage was around render expectation(in view specs). Every spec doing something similar to this…

1
2
3
4
5
  it "should do something" do
    template.expects(:render).with(:partial => 'foo', :locals => @bar).times(2).returns('foo')
    render 'view_path/to_template'
    response.body.should have_text(/foo/)
  end

….was confusing the ‘effective’ render call in line 3 with the expectation in line 2, and was raising expectation mismatch errors, which left me with no way to successfully validate an expectation.

This is the quick fix i added to spec/spec_helper.rb… which did the trick, and the spec(post the fix), looks like….

1
2
3
4
5
6
  it "should do something" do
    template.set_render_expectation(:with => [{:partial => 'foo', :locals => @bar}], :return => "foo", :times => 2)
    render 'view_path/to_template'
    response.body.should have_text(/foo/)
    template.assert_render_expectation_satisfied!
  end

I hope this quick hack will help someone… Happy Hacking… :-)

About these ads

About this entry