Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 921
Sean Page Avatar asked Aug 05 '26 23:08

Sean Page


1 Answers

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
like image 101
Dave Schweisguth Avatar answered Aug 08 '26 16:08

Dave Schweisguth