我有这个测试:
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
最佳答案
方括号表示数组。看起来stories(:promoted)
仅返回一个故事,而assigns(:stories)
返回一个包含该故事的length-1数组。
关于ruby-on-rails - Rails中的断言参数上的方括号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5438107/