RSpec 3 RuntimeError: "let declaration accessed in a `before(:context)` hook"
Asked Answered
A

1

7

Here is my error

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

and here is the code where it's breaking

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

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

I've tried removing them and moving them around but no success.

Aryn answered 22/4, 2016 at 13:35 Comment(1)
Possible duplicate of In RSpec, using let variable inside before :all blockBailment
P
6

You can't refer to a let variable (or subject) in a before(:all)/before(:context) hook. Doing so was deprecated in RSpec 2 and removed from RSpec 3.

In your case it looks like you can just inline the let variable into the before(:all) block:

before(:all) do
  @queue = FactoryGirl.create(described_class.to_s.underscore.to_sym)
end
Parker answered 22/4, 2016 at 13:54 Comment(3)
Awesome that fixed it. Thank you!Aryn
This is interesting... I wonder why they did this. Too bad it's gone, because now I have to initialise the thing for every single test, instead of just the contexts where it was relevant. :/Breakneck
And now I find even weirder shit going on - even though you can no longer call them from before(:context), rspec seemingly is keeping the value across examples and not calling the block again during the second example. RSpec is getting really confusing...Breakneck

© 2022 - 2024 — McMap. All rights reserved.