0

I'm trying to use Rspec to loop through a series of very similar tests, but it's not working out for me right now. I have a hash of states and I want to test all of them, but I don't know the order they'll be in, so I've set it up like this:

before(:all) do
  @hash = {
    state_1 => {'item1' => 'a', 'item2' => 'b', 'item3' => 'c'},
    state_2 => {'item1' => 'd', 'item2' => 'e', 'item3' => 'f'},
    state_3 => {'item1' => 'g', 'item2' => 'h', 'item3' => 'i'}
  }
end

until @hash.empty? do
  context 'code does some stuff' do
    before(:all) do
      @state = <result of state determining function>
      @item1 = @hash[@state]['item1']
      @item2 = @hash[@state]['item2']
      @item3 = @hash[@state]['item3']
    end
    it 'does some stuff' do
      ...
    end
    after(:all) do
      @hash.delete(@state)
    end
  ...
end

When I run my rspec tests, however, I get an error that there is no method 'empty?' for nil:NilClass. So questions are, what am I doing wrong, and what is the preferred way to do something of this sort?

Thanks!

1 Answer 1

1

it looks like you're looking to use let, which recreates the variable for each context

Sign up to request clarification or add additional context in comments.

1 Comment

I got rid of the before and instead used let(:hash) { {state_1 => .....}, and I changed all occurrences of @hash to hash, and now I get a different error: undefined local variable or method, NameError

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.