Rails 中断言参数的方括号
我有这个测试:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
方括号表示一个数组。看起来
stories(:promoted)
仅返回一个故事,而assigns(:stories)
返回一个包含该故事的 length-1 数组。The square brackets indicates an array. Looks like
stories(:promoted)
is returning just one story, whereasassigns(:stories)
returns a length-1 array containing that story.