RSPEC对象验证失败...在`:

发布于 2025-02-12 14:04:21 字数 548 浏览 0 评论 0原文

我已经写了一个测试,看起来很接近,但不是很正确。

it "adds the correct permissions" do
  subject
  expect(policy.permissions).to have_key("users")
  expect(policy.permissions["users"]).to eq({ "all_users" => {
    can_view: false,
    can_manage: false,
  } })
end

输出:

expected: {"all_users"=>{:can_manage=>false, :can_view=>false}}
     got: {"all_users"=>{"can_manage"=>false, "can_view"=>false}}
     

显然我很接近,但是我不确定的交易是什么:符号与“ got” got“ got”输出。我该如何通过?

I've written a test and it seems so close but not quite right.

it "adds the correct permissions" do
  subject
  expect(policy.permissions).to have_key("users")
  expect(policy.permissions["users"]).to eq({ "all_users" => {
    can_view: false,
    can_manage: false,
  } })
end

Output:

expected: {"all_users"=>{:can_manage=>false, :can_view=>false}}
     got: {"all_users"=>{"can_manage"=>false, "can_view"=>false}}
     

Obviously I'm close but I'm not sure what the deal is in terms of the : notation versus the "got" output from the test itself. How do I get this to pass?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦行七里 2025-02-19 14:04:21

字符串键在哈希类中不等于符号键。这是以下示例代码:

hash = {}
hash[:a] = 1
hash['b'] = 2

hash[:a] # returns 1
hash['a'] # returns nil

hash[:b] # returns nil
hash['b'] #returns 2

因此,您应该期望这样的结果:

expect(policy.permissions["users"]).to eq({ 'all_users' => { 'can_view' => false, 'can_manage' => false, } })

String keys are not equal to symbol keys in Hash class. Here is the example code below:

hash = {}
hash[:a] = 1
hash['b'] = 2

hash[:a] # returns 1
hash['a'] # returns nil

hash[:b] # returns nil
hash['b'] #returns 2

so you should expect the result like this:

expect(policy.permissions["users"]).to eq({ 'all_users' => { 'can_view' => false, 'can_manage' => false, } })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文