Rails中Assert参数的方括号

时间:2011-03-25 21:00:37

标签: ruby-on-rails testing functional-testing

我有这个测试:

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>]>.

然而,如果我在“故事(:提升)”param

周围放置方括号
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

1 个答案:

答案 0 :(得分:2)

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