Alex Rivera | Logout

Speeding up rspec controllers test: using before all fails?

Asked 2011-10-06T15:34:27.260
12

I have a simple controller test, containing a.o. the following code:

context "POST :create" do
  before (:each) do
    post :create, :user_id => @user.id,
         :account => { .. some data ... }
  end
  it { response.status.should == 201 }
  it { response.location.should be_present }
end

Now I thought of a very simple way to speed up this test, and to use a before(:all) instead of a before(:each). In that case the post would only be done once.

So i wrote:

context "POST :create" do
  before (:all) do
    post :create, :user_id => @user.id,
         :account => { .. some data ... }
  end
  it { response.status.should == 201 }
  it { response.location.should be_present }
end

But then I get the following errors:

 RuntimeError:
   @routes is nil: make sure you set it in your test's setup method.

Is this by design? Is there a way to circumvent it?

Edit
Report

1 Answer

12

I asked this question on the rspec mailing list, and got the following reply from @dchelimsky himself:

Yes. rspec-rails wraps the rails' testing framework which doesn't have a before(:all) concept in it, so all the data is reset before each example. Even if we wanted to support this in rspec-rails (which I don't) it would require changes to rails first.

So doing controller calls is not possible in a before(:all), it can only be used to setup your DB or instance variables.

answered 2012-04-07T07:35:46.017

Your Answer