2014-10-30 31 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

相關問題