2014-10-30 64 views
0

我试图使用Rspec来循环一系列非常相似的测试,但它现在不适合我。我有状态的哈希值,我想测试所有的人,但我不知道他们会在订货,所以我将它设置这样的:Rspec可以在循环构造中使用实例变量吗?

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 

当我跑我的然而,rspec测试,我得到一个错误,没有方法'空?'。为零:NilClass。所以问题是,我做错了什么,做这种事情的首选方式是什么?

谢谢!

回答

1

它看起来像你希望使用let,它再现了变量为每个上下文

+0

我摆脱了之前的和替代使用让(:哈希){{STATE_1 => .....} ,并且我将所有出现的@hash改为hash,现在我得到一个不同的错误: 未定义的局部变量或方法,NameError – carlmonday 2014-10-30 21:04:54

相关问题