6

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.

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Sean Page
  • 63
  • 1
  • 4
  • 3
    Possible duplicate of [In RSpec, using let variable inside before :all block](https://stackoverflow.com/questions/19670375/in-rspec-using-let-variable-inside-before-all-block) – kolen Apr 17 '19 at 19:59

1 Answers1

5

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
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
  • Awesome that fixed it. Thank you! – Sean Page Apr 22 '16 at 14:26
  • 1
    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. :/ – Hakanai Nov 27 '16 at 22:42
  • 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... – Hakanai May 25 '17 at 01:15