Scope constants to an rspec context
Asked Answered
B

3

24

I often want to do

context "empty stack" do
  SOME_CONSTANT = "value"
  it "should be empty" do
    # use SOME_CONSTANT
  end
end

context "populated stack" do
  SOME_CONSTANT = "a different value"
  it "should have some items" do
    # use SOME_CONSTANT
  end
end

ruby doesn't scope constants to closures so they leak out. Does anyone have a trick for declaring constants that are scoped to a context?

Bodkin answered 8/3, 2011 at 8:31 Comment(0)
S
32

Change the declaration of the constant:
from SOME_CONSTANT = "value"
to self::SOME_CONSTANT = "value"

RSpec creates an anonymous class for each set of specs (context in your example) that it comes across. Declaring a constant without self:: in an anonymous class makes it available in global scope, and is visible to all the specs. Changing the constant declaration to self:: ensures that it is visible only within the anonymous class.

Swoosh answered 17/5, 2011 at 1:26 Comment(1)
To use those constants inside an it block do: self.class::SOME_CONSTANTGrapery
B
25

Having used rspec for longer now I think the more idiomatic approach is to use let.

context "empty stack" do
  let(:some_constant){ "value" }

  it "should be empty" do
    puts some_constant
  end
end

context "populated stack" do
  let(:some_constant){ "a different value" }

  it "should have some items" do
    puts some_constant
  end
end
Bodkin answered 12/4, 2013 at 17:43 Comment(4)
I totally recommend using the let constructs. Read more about let here: relishapp.com/rspec/rspec-core/docs/helper-methods/let-and-letGurl
I want to use let but it's not available in the before blocks.Messere
That's not true. Definitions with let are available in before, before(:each) and before(:suite). They only don't work with before(:all).Bernie
before(:all) exact the case why I am here :)Whitmer
A
2

The correct way to do it is to use stub_const

RSpec.describe "stubbing FOO" do
  it "can stub undefined constant FOO" do
    stub_const("FOO", 5)
    expect(FOO).to eq(5)
  end

  it "undefines the constant when the example completes" do
    expect { FOO }.to raise_error(NameError)
  end
end

https://relishapp.com/rspec/rspec-mocks/v/3-9/docs/mutating-constants/stub-undefined-constant

Agueda answered 20/6, 2021 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.