49

Suppose I have the following spec:

describe Thing do
  it 'can read data' do
    @data = get_data_from_file  # [ '42', '36' ]
    expect(@data.count).to eq 2
  end

  it 'can process data' do
    expect(@data[0].to_i).to eq 42  # Fails because @data is nil
  end
end

All I wanted is to have a variable shared in the given describe or context. I would write a value in one example and read it in another. How do I do that?

1
  • did you get a good solution for this? Sep 1, 2023 at 22:01

3 Answers 3

65

You should use before(:each) or before(:all) block:

describe Thing do
  before(:each) do
    @data = get_data_from_file  # [ '42', '36' ]
  end

  it 'can read data' do
    expect(@data.count).to eq 2
  end

  it 'can process data' do
    expect(@data[0].to_i).to eq 42
  end
end

The difference is that before(:each) will be executed for each case separately and before(:all) once before all examples in this describe/context. I would recommend you to prefer before(:each) over before(:all), because each example will be isolated in this case which is a good practice.

There are rare cases when you want to use before(:all), for example if your get_data_from_file has a long execution time, in this case you can, of course, sacrifice tests isolation in favor of speed. But I want to aware you, that when using before(:all), modification of your @data variable in one test(it block) will lead to unexpected consequences for other tests in describe/context scope because they will share it.

before(:all) example:

describe MyClass do
  before(:all) do
    @a = []
  end

  it { @a << 1; p @a }
  it { @a << 2; p @a }
  it { @a << 3; p @a }
end

Will output:

[1]
[1, 2]
[1, 2, 3]

UPDATED

To answer you question

describe MyClass do
  before(:all) do
    @a = []
  end

  it { @a = [1]; p @a }
  it { p @a }
end

Will output

[1]
[]

Because in first it you are locally assigning instance variable @a, so it isn't same with @a in before(:all) block and isn't visible to other it blocks, you can check it, by outputting object_ids. So only modification will do the trick, assignment will cause new object creation.

So if you are assigning variable multiple times you should probably end up with one it block and multiple expectation in it. It is acceptable, according to best practices.

6
  • Thank you. The idea is that I want to set a variable in one example and read/update it in another within the same 'describe' or 'context' block. For some reason the solution you proposed isn't working. Dec 28, 2014 at 19:08
  • @AndreyEsperanza did you try before(:all)? Dec 28, 2014 at 19:09
  • Yep. Of course, I gave an overly simplified example. Now I'm figuring out what can be wrong with the real spec. Dec 28, 2014 at 19:12
  • @AndreyEsperanza I've just tested and added corresponding example to my answer. Maybe you should show us your code. Dec 28, 2014 at 19:23
  • Your example works, but this is not what I need. How about this: it { @a = [1] }; it { p @a } # [], not [1] Dec 28, 2014 at 19:37
17

This is really the purpose of the RSpec let helper which allows you to do this with your code:

...
describe Thing do
  let(:data) { get_data_from_file }

  it 'can read data' do
     expect(data.count).to eq 2
  end

  it 'can process data' do
     expect(data[0].to_i).to eq 42
  end

end
...
1
  • 2
    Thanks for your answer, but as you see from the question above, the idea is to set a variable in one example and read/update it in another, within the same 'describe' or 'context' block. Dec 28, 2014 at 19:09
0

I just ran into this same problem. How I solved it was by using factory_girl gem.

Here's the basics:

create a factory (here's a code snippet:

require 'factory_girl'
require 'faker' # you can use faker, if you want to use the factory to generate fake data

FactoryGirl.define do
  factory :generate_data, class: MyModule::MyClass do
    key 'value'
  end
end

Now after you made the factory you need to make a Model that looks like this:

Module MyModule
  class MyClass
    attr_accessor :key

    #you can also place methods here to call from your spec test, if you wish
    # def self.test
        #some test
    # end
  end
end

Now going back to your example you can do something like this:

describe Thing do
  before(:all) do
  @data = FactoryGirl.build(:generate_data)
  end

  it 'can read data' do
     @data.key = get_data_from_file  # [ '42', '36' ]
     expect(@data.key.count).to eq 2
  end

  it 'can process data' do
     expect(@data.key[0].to_i).to eq 42  # @data will not be nil. at this point. whatever @data.key is equal to last which was set in your previous context will be what data.key is here
  end

end

Anyways, good luck let us know, if you got some other solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.