ruby-on-rails – 加速rspec控制器测试:使用之前都失败?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 加速rspec控制器测试:使用之前都失败?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个简单的控制器测试,包含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.

所以在以前(:(全部))中执行控制器调用是不可能的,它只能用于设置数据库或实例变量.

原文链接:https://www.f2er.com/ruby/271006.html

猜你在找的Ruby相关文章