Friday, January 21, 2011

RSpec 2 changes from RSpec 1



A quick list of changes we've discovered in moving from RSpec 2 to RSpec 1. Some may produce deprecation warnings -- other changes simply fail the test. Please post anything you discover that's not on this list and I'll add it.

RSpec 1.xRSpec 2.x
GENERAL
@value.should be_close(300.0/72, 1e-4)@value.should be_within(1e-4).of(300.0/72)
 
CONTROLLER SPECS
integrated_views render_views
reverse order of assertion and HTTP verb method:
controller.should render_template("edit")
get :edit, :name => "fred"
get :edit, :name => "fred"
response.should render_template("edit")
assigns[:instance_var].shouldassigns(:instance_var).should
parameter hash values can be numericparameter hash values must be strings
put :update, :user_id => 3put :update, :user_id => "3"
view template file not requiredtemplate file required or set expectation: controller.should_receive(:render) -- or stub it: controller.stub!(:render)
 
VIEW SPECS
assigns[:instance_var] = valueassign(:instance_var, value)
response.bodyrendered
include_textinclude
have_taghave_selector
with_taghave_selector
:text:content
have_tag arguments don't need quotesnumbers or strings beginning with / must be quoted
escape URL stringsdo not escape URL strings
response.should have_tag('form[action=/docs/111?id=44&x=1]') do
   with_tag 'input[type=hidden][name=x][value=55]'
end
rendered.should have_selector('form[action="/docs/111?id=44&x="1"]') do |tags|
   tags.should have_selector 'input[type=hidden][name=x][value="55"]'
end
response.should have_tag('td') do
   with_tag 'span', :text => "Frank N. Stein"
end
rendered.should have_selector('td') do |tags|
   tags.should have_selector 'span', :content => "Frank N. Stein"
end
 
For more information on changes from have_tag to have_selector, check this out.
 
ROUTING SPECS
{:get => 'all/data/in/db'}.should_not be_routeable{:get => 'all/data/in/db'}.should_not be_routable
route_for no longer supported
assertions agains query parameters no longer supported:
{:get => 'c/a?query_param=value'}.should route_to(:controller => 'c', :action => 'a', :query_param => 'value')
{:get => 'c/a?query_param=value'}.should route_to(:controller => 'c', :action => 'a')
 
MOCKS & STUBS
stub_model(Widget, :new_record? => true)stub_model(Widget).as_new_record