gpt4 book ai didi

ruby-on-rails - 加速 rspec Controller 测试 : using before all fails?

转载 作者:行者123 更新时间:2023-12-04 00:48:09 24 4
gpt4 key购买 nike

我有一个简单的 Controller 测试,包含 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, :user_id => @user.id,
: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.



所以在 before(:all) 中不可能进行 Controller 调用,它只能用于设置您的数据库或实例变量。

关于ruby-on-rails - 加速 rspec Controller 测试 : using before all fails?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7676728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com