Rspec控制器测试分配二维哈希失败

时间:2016-11-14 23:47:33

标签: ruby-on-rails ruby hash rspec controller

我正在尝试运行控制器测试: 控制器代码是

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

结果是

  

故障:

1)AixController应该显示aix_service_accounts      失败/错误:期望(指定(: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)>'

有人可以帮助并向我解释原因吗?谢谢!

1 个答案:

答案 0 :(得分:0)

当然 - 您正在尝试从分配中请求一个名为:no_dn_users[0]['aix_username']的密钥。您需要将其更改为:

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

代替。

即,通过密钥:no_dn_users获取第一部分,这将是您使用索引引用的哈希内部数组