Rails 中断言参数的方括号

发布于 2024-10-26 21:28:30 字数 921 浏览 1 评论 0原文

我有这个测试:

def test_should_only_list_promoted_on_index
    get :index
    assert_equal stories(:promoted), assigns(:stories)
  end

失败并显示消息:

<#<Story id: 3, name: "What is a Debugger?", link: "http://en.wikipedia.org/wiki/Debugger", created_at: "2011-03-25 20:57:04", updated_at: "2011-03-25 20:57:04", user_id: 2, votes_count: 5, description: nil>> expected but was
<[#<Story id: 3, name: "What is a Debugger?", link: "http://en.wikipedia.org/wiki/Debugger", created_at: "2011-03-25 20:57:04", updated_at: "2011-03-25 20:57:04", user_id: 2, votes_count: 5, description: nil>]>.

但是,如果我在“stories(:promoted)”参数周围加上方括号,则

def test_should_only_list_promoted_on_index
    get :index
    assert_equal [stories(:promoted)], assigns(:stories)
  end

测试会成功。这是为什么呢?

我正在使用 Rails 2.3.9 和 Ruby 1.9.2

I have this test:

def test_should_only_list_promoted_on_index
    get :index
    assert_equal stories(:promoted), assigns(:stories)
  end

which fails with the message:

<#<Story id: 3, name: "What is a Debugger?", link: "http://en.wikipedia.org/wiki/Debugger", created_at: "2011-03-25 20:57:04", updated_at: "2011-03-25 20:57:04", user_id: 2, votes_count: 5, description: nil>> expected but was
<[#<Story id: 3, name: "What is a Debugger?", link: "http://en.wikipedia.org/wiki/Debugger", created_at: "2011-03-25 20:57:04", updated_at: "2011-03-25 20:57:04", user_id: 2, votes_count: 5, description: nil>]>.

however if I put square braces around the "stories(:promoted)" param

def test_should_only_list_promoted_on_index
    get :index
    assert_equal [stories(:promoted)], assigns(:stories)
  end

the test succeeds. Why is this?

I am using Rails 2.3.9 and Ruby 1.9.2

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

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

发布评论

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

评论(1

煮酒 2024-11-02 21:28:30

方括号表示一个数组。看起来 stories(:promoted) 仅返回一个故事,而 assigns(:stories) 返回一个包含该故事的 length-1 数组。

The square brackets indicates an array. Looks like stories(:promoted) is returning just one story, whereas assigns(:stories) returns a length-1 array containing that story.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文