RSpec 3 RuntimeError:“允许在`before(:context)`hook中访问声明”

时间:2016-04-22 13:35:55

标签: ruby-on-rails ruby rspec rspec-rails rspec3

这是我的错误

Failure/Error: @queue = FactoryGirl.create(model.to_s.underscore.to_sym)
 RuntimeError:
   let declaration `model` accessed in a `before(:context)` hook at:
     /var/www/html/SQ-UI/spec/support/user_queue/asterisk_serialize_spec.rb:7:in `block (2 levels) in <top (required)>'

   `let` and `subject` declarations are not intended to be called
   in a `before(:context)` hook, as they exist to define state that
   is reset between each example, while `before(:context)` exists to
   define state that is shared across examples in an example group.enter code here

以下是破解的代码

let(:model) { described_class } # the class that includes the concern

before(:all) do
  @queue = FactoryGirl.create(model.to_s.underscore.to_sym)
end

我试过移除它们并移动它们但没有成功。

1 个答案:

答案 0 :(得分:1)

您无法在let / subject挂钩中引用before(:all)变量(或before(:context))。这样做在RSpec 2中已弃用,并从RSpec 3中删除。

在您的情况下,您似乎可以将let变量内联到before(:all)块中:

before(:all) do
  @queue = FactoryGirl.create(described_class.to_s.underscore.to_sym)
end