我是使用RSPEC测试控制器的新手。目前我正在测试我的作业是否正确。我的控制器动作(我试图测试的)看起来像这样:
def jobs
@jobs = @customer.jobs
end
我的测试看起来像这样:
describe CustomersController do
before do
@company = FactoryGirl.create(:company_complete)
@account = FactoryGirl.create(:account_house_address)
end
it "assigns @jobs" do
get :jobs
customer = FactoryGirl.create(:customer_john_human)
job = FactoryGirl.create(:job_complete_job, account_id: @account.id, company_id: @company.id, customer_id: customer.id )
expect(assigns(:jobs)).to eq([customer.jobs])
end
然而,当我运行这个时,我得到错误:
Failure/Error: expect(assigns(:jobs)).to eq([customer.jobs])
expected: [#<ActiveRecord::Associations::CollectionProxy [#<Job id: 1, name: nil, status: "pending", company_id: 1, account_id_old: nil, job_type_id: nil, address_id: nil, trade_id: 1, lead_id: nil, started_date: nil, end_date: nil, created_at: "2015-04-17 03:58:42", updated_at: "2015-04-17 03:58:42", order_number: 2, creator_id: nil, account_id: 1, customer_id: 1, contact_id: nil>]>]
got: nil
我的完整客户控制器如下所示:
class CustomersController < ApplicationController
before_action :authenticate_member!
load_and_authorize_resource
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, :alert => exception.message
end
before_action :set_section
before_action :set_client_form, except: [:index]
before_action :set_customer, only: [:show, :jobs, :notes, :destroy]
helper_method :selected_show_tab
def index
#got rid of this to make it easier to read :p
end
def jobs
@jobs = @customer.jobs
end
编辑:我添加了以下案例来验证用户身份:
before :each do
controller.stub(:authenticate_member!)
end
然而,我仍然得到相同的结果:(