2017-02-27 86 views
0

首先,我是新来使用RSpec的和较新的到Rails,所以我还是找出了很多这方面的技术是如何工作的相比,我过去的语言经验。RSpec的包括未能找到对象

的问题,RSpec的失败试验,我看不出它是什么,我失踪。

该测试是测试控制器的GET索引。

测试:

it "populates an array of profiles" do 
    profile = create(:profile) 
    get :index 
    expect(:profiles).to include(profile) 
end 

结果:

1) ProfilesController GET #index populates an array of profiles 
Failure/Error: expect(:profiles).to include(profile) 

    expected :profiles to include #<Profile id: 3, user_id: 2, name: "Kasandra Goldner", about: nil, country_code: nil, state_code: nil...15", avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil>, but it does not respond to `include?` 
    Diff: 
    @@ -1,2 +1,2 @@ 
    -[#<Profile id: 3, user_id: 2, name: "Kasandra Goldner", about: nil, country_code: nil, state_code: nil, city: nil, created_at: "2017-02-27 03:42:15", updated_at: "2017-02-27 03:42:15", avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil>] 
    +:profiles 

# ./spec/controllers/profiles_controller_spec.rb:15:in `block (3 levels) in <top (required)>' 

谢谢。

回答

0

尝试在受让人包装:profiles获得其在控制器设置实例VAR:

expect(assigns(:profiles)).to include(profile) 
+0

感谢克里斯说做到了! –