我的CompaniesController
class CompaniesController < ApplicationController
def new
@company = Company.new
@company.employees.build
end
在我的Rspec中,我如何检查在build
操作中调用new
方法?
我试过这样的事情
expect(assigns(:company).employees).to have_received(:build)
但是我收到以下错误消息:
#<Employee::ActiveRecord_Associations_CollectionProxy:0x007fe2ca2141e8> expected to have received build, but that object is not a spy or method has not been stubbed.
我是否需要先雇员?
Rspec for new action
describe "GET 'new'" do
before { get :new }
it 'assigns @domain_name' do
expect(assigns(:domain_name)).to eq(request.host)
end
it 'assigns @company' do
expect(assigns(:company)).to be_a_new(Company)
end
it 'renders the new template' do
expect(response).to render_template('new')
end
端
由于
答案 0 :(得分:3)
你可以这样做
before { expect_any_instance_of(Company.new.employees.class).to receive(:build) }
答案 1 :(得分:1)
如何检查结果而不是检查方法调用是否发生?
it 'builds an employee on @company' do
expect(assigns(:company).employees.length).to eq(1)
# and/or
expect(assigns(:company).employees.first).to be_a_new(Employee)
end