【问题标题】:RSpec 3 RuntimeError: "let declaration accessed in a `before(:context)` hook"RSpec 3 RuntimeError:“让声明在 `before(:context)` 钩子中访问”
【发布时间】:2016-04-22 13:35:55
【问题描述】:

这是我的错误

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

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

【问题讨论】:

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


【解决方案1】:

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

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

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

【讨论】:

  • 修复它太棒了。谢谢!
  • 这很有趣......我想知道他们为什么这样做。太糟糕了,它已经消失了,因为现在我必须为每个测试初始化​​这个东西,而不仅仅是它相关的上下文。 ://
  • 现在我发现更奇怪的事情发生了——即使你不能再从before(:context) 调用它们,rspec 似乎在示例中保持值并且在第二个示例中不再调用块。 RSpec 变得非常混乱......
猜你喜欢
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多