2016-11-14 51 views
0

我想要運行一個控制器測試: 控制器代碼是Rspec的控制器測試分配二維散列失敗

def aix_service_accounts 
    @no_dn_users= Hash.new 
    record = 0 
    @no_dn_users[record]= Hash.new 
    @no_dn_users[record]['aix_username'] ="abc" 
end 

的rspec的代碼是

it "should show aix_service_accounts" do 
    get :aix_service_accounts 
    expect(assigns(:no_dn_users[0]['aix_username'])).to eq 'abc' 
    end 

結果是

Failures:

1)AixController應顯示aix_service_accounts Failu RE /錯誤:期待(受讓人(:no_dn_users [0] [ 'aix_username'])),以EQ 'ABC'

expected: "abc" 
     got: {"marked_for_same_origin_verification"=>true, "no_dn_users"=>{0=>{"aix_username"=>"abc"}}} 

    (compared using ==) 

    Diff: 
    @@ -1,2 +1,3 @@ 
    -"abc" 
    +"marked_for_same_origin_verification" => true, 
    +"no_dn_users" => {0=>{"aix_username"=>"abc"}}, 

# ./spec/controllers/aix_controller_spec.rb:29:in `block (2 levels) in <top (required)>' 
莫非

任何人的幫助,並說明理由給我?謝謝!

回答

0

當然 - 你試圖從指定要求一個名爲:no_dn_users[0]['aix_username']的鑰匙。您需要將其更改爲:

assigns(:no_dn_users)[0]['aix_username'] 

改爲。

即由關鍵:no_dn_users走出的第一部分,這將是哈希內包括陣列,你再使用索引

+1

感謝泰倫引用,你是絕對正確的。 –