我有一个简单的控制器测试,包含a.o.以下代码:
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
现在我想到了一个非常简单的方法来加速这个测试,并且使用before(:all)而不是before(:each).在这种情况下,该职位只会做一次.
所以我写道:
context "POST :create" do before (:all) do post :create,:account => { .. some data ... } end it { response.status.should == 201 } it { response.location.should be_present } end
但是,我收到以下错误:
RuntimeError: @routes is nil: make sure you set it in your test's setup method.
这是设计吗?有办法规避吗?
解决方法
我在rspec邮件列表中提出了这个问题,并从@dchelimsky自己得到以下答复:
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.