rspec 期望所有 :resources 等于 resource
rspec expects all :resources to equal resource
我是RSpec的新手,我想知道为什么我没有通过这个测试
before(:each) { get :index }
it "assigns all favorites as @favorites" do
favorite = FactoryGirl.create(:favorite)
expect(assigns(:favorites)).to eq([favorite])
end
它说
1) FavoritesController GET index assigns all favorites as @favorites
Failure/Error: expect(assigns(:favorites)).to eq([favorite])
expected: [#<Favorite id: 1, patient_id: 6, doctor_id: 5>]
got: #<ActiveRecord::Relation []>
(compared using ==)
Diff:
@@ -1,2 +1,2
-[#<Favorite:0x000000058a5ca0 id: 1, patient_id: 6, doctor_id: 5>]
+[]
似乎 assigns(:favorites)
空了。我也尝试过另一种方法
def valid_attributes
doctor = FactoryGirl.create(:doctor)
patient = FactoryGirl.create(:patient)
FactoryGirl.attributes_for(:favorite, doctor_id: doctor.id, patient_id: patient.id)
end
it "assigns all favorites as @favorites" do
favorite = Favorite.create! valid_attributes
expect(assigns(:favorites)).to eq([favorite])
end
它得到了同样的错误。任何输入都会对我有帮助,我想问一下是否有任何方法可以简化它。
更新
app/controllers/favorite_controller.rb
class FavoritesController < ApplicationController
before_action :set_favorite, only: [:destroy]
before_action :authenticate_user!
def index
@favorites = Favorite.where(:patient_id => current_user.id).order(id: :asc)
end
end
spec/controllers/favorite_controller_spec.rb
require 'spec_helper'
describe FavoritesController, type: :controller do
login_patient
describe "GET index" do
let!(:favorite) { FactoryGirl.create(:favorite) }
before { get :index }
it { expect(response).to render_template(:index) }
it { expect(response).to be_success }
it { expect(response).to have_http_status(200) }
it "blocks unauthenticated access", :skip_before do
expect(response).to redirect_to(new_user_session_path)
end
it "assigns all favorites as @favorites" do
expect(assigns(:favorites).to_a).to eq([favorite])
end
end
end
spec/support/controller_helpers.rb
module ControllerHelpers
def login_patient
before :each do |example|
unless example.metadata[:skip_before]
@request.env["devise.mapping"] = Devise.mappings[:user]
@patient = FactoryGirl.create(:patient)
sign_in :user, @patient
end
end
end
end
尝试在请求之前创建记录:
let!(:favorite) { FactoryGirl.create(:favorite) }
before(:each) { get :index }
it "assigns all favorites as @favorites" do
expect(assigns(:favorites).to_a).to eq([favorite])
end
您是在发送请求后创建记录,因此在请求完成时,您刚刚创建的记录不会包含在收藏夹列表中。将您的测试代码更改为以下内容
let!(:favorite) { FactoryGirl.create(:favorite) }
before { get :index }
it "assigns all favorites as @favorites" do
expect(assigns(:favorites)).to eq([favorite])
end
这可能仍然会失败,因为 assigns(:favorites)
是一个 ActiveRecord::Relation
object 所以你必须调用 to_a
expect(assigns(:favorites).to_a).to eq([favorite])
更新:
由于收藏是按患者筛选的,所以在测试中要确保创建的收藏是属于患者的。您可以通过将收藏夹更改为
来做到这一点
let!(:favorite) { FactoryGirl.create(:favorite, patient: @patient)
我是RSpec的新手,我想知道为什么我没有通过这个测试
before(:each) { get :index }
it "assigns all favorites as @favorites" do
favorite = FactoryGirl.create(:favorite)
expect(assigns(:favorites)).to eq([favorite])
end
它说
1) FavoritesController GET index assigns all favorites as @favorites
Failure/Error: expect(assigns(:favorites)).to eq([favorite])
expected: [#<Favorite id: 1, patient_id: 6, doctor_id: 5>]
got: #<ActiveRecord::Relation []>
(compared using ==)
Diff:
@@ -1,2 +1,2
-[#<Favorite:0x000000058a5ca0 id: 1, patient_id: 6, doctor_id: 5>]
+[]
似乎 assigns(:favorites)
空了。我也尝试过另一种方法
def valid_attributes
doctor = FactoryGirl.create(:doctor)
patient = FactoryGirl.create(:patient)
FactoryGirl.attributes_for(:favorite, doctor_id: doctor.id, patient_id: patient.id)
end
it "assigns all favorites as @favorites" do
favorite = Favorite.create! valid_attributes
expect(assigns(:favorites)).to eq([favorite])
end
它得到了同样的错误。任何输入都会对我有帮助,我想问一下是否有任何方法可以简化它。
更新
app/controllers/favorite_controller.rb
class FavoritesController < ApplicationController
before_action :set_favorite, only: [:destroy]
before_action :authenticate_user!
def index
@favorites = Favorite.where(:patient_id => current_user.id).order(id: :asc)
end
end
spec/controllers/favorite_controller_spec.rb
require 'spec_helper'
describe FavoritesController, type: :controller do
login_patient
describe "GET index" do
let!(:favorite) { FactoryGirl.create(:favorite) }
before { get :index }
it { expect(response).to render_template(:index) }
it { expect(response).to be_success }
it { expect(response).to have_http_status(200) }
it "blocks unauthenticated access", :skip_before do
expect(response).to redirect_to(new_user_session_path)
end
it "assigns all favorites as @favorites" do
expect(assigns(:favorites).to_a).to eq([favorite])
end
end
end
spec/support/controller_helpers.rb
module ControllerHelpers
def login_patient
before :each do |example|
unless example.metadata[:skip_before]
@request.env["devise.mapping"] = Devise.mappings[:user]
@patient = FactoryGirl.create(:patient)
sign_in :user, @patient
end
end
end
end
尝试在请求之前创建记录:
let!(:favorite) { FactoryGirl.create(:favorite) }
before(:each) { get :index }
it "assigns all favorites as @favorites" do
expect(assigns(:favorites).to_a).to eq([favorite])
end
您是在发送请求后创建记录,因此在请求完成时,您刚刚创建的记录不会包含在收藏夹列表中。将您的测试代码更改为以下内容
let!(:favorite) { FactoryGirl.create(:favorite) }
before { get :index }
it "assigns all favorites as @favorites" do
expect(assigns(:favorites)).to eq([favorite])
end
这可能仍然会失败,因为 assigns(:favorites)
是一个 ActiveRecord::Relation
object 所以你必须调用 to_a
expect(assigns(:favorites).to_a).to eq([favorite])
更新:
由于收藏是按患者筛选的,所以在测试中要确保创建的收藏是属于患者的。您可以通过将收藏夹更改为
来做到这一点let!(:favorite) { FactoryGirl.create(:favorite, patient: @patient)