繁体   English   中英

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

[英]Rspec controller test assign two dimensional Hash fails

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

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失败/错误:Expect(assigns(:no_dn_users [0] ['aix_username']))。to 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)>'

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

当然-您正尝试从:no_dn_users[0]['aix_username']请求一个名为:no_dn_users[0]['aix_username'] 您需要将其更改为:

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

代替。

即,通过键:no_dn_users取出第一部分,这将是哈希内部数组,然后使用索引进行引用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM