【问题标题】:Rspec controller test assign two dimensional Hash failsRspec 控制器测试分配二维哈希失败
【发布时间】:2016-11-14 23:47:32
【问题描述】:

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

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)>'

谁能帮我解释一下原因?谢谢!

【问题讨论】:

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


    【解决方案1】:

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

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

    改为。

    即,通过键 :no_dn_users 取出第一部分,这将是您随后使用索引引用的 hash-inside-an-array

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 2014-08-27
      • 2014-02-11
      • 1970-01-01
      相关资源
      最近更新 更多