RSpec包括找不到对象

时间:2017-02-27 03:56:22

标签: ruby-on-rails-4 rspec

首先,我是使用RSpec并且对Rails相对较新的新手,所以我仍然在研究这些技术与我过去的语言经验相比如何运作。

问题是,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)>'

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试在分配中包装:profiles以获取在控制器中设置的实例var:

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