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!