2011-03-25 158 views
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>]>. 
但是如果我把方括号围绕

“(:提拔)的故事”参数

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

回答

2

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