加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:26:24 所属栏目:百科 来源:网络整理
导读:我有一个简单的控制器测试,包含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
我有一个简单的控制器测试,包含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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读