我有以下课程:
class Company < ActiveRecord::Base validates :name, :presence => true has_many :employees, :dependent => :destroy end class Employee < ActiveRecord::Base validates :first_name, :presence => true validates :last_name, :presence => true validates :company, :presence => true belongs_to :company end
我正在为Employee
课程编写测试,因此我试图创建double
供Company
所使用Employee
。
以下是我的Rspec的代码段
let(:company) { double(Company) } let(:employee) { Employee.new(:first_name => 'Tom', :last_name => 'Smith', :company => company) } context 'valid Employee' do it 'will pass validation' do expect(employee).to be_valid end it 'will have no error message' do expect(employee.errors.count).to eq(0) end it 'will save employee to database' do expect{employee.save}.to change{Employee.count}.from(0).to(1) end end
我的所有3个测试都收到以下错误消息
ActiveRecord::AssociationTypeMismatch: Company(#70364315335080) expected, got RSpec::Mocks::Double(#70364252187580)
我认为我尝试创建的方式double
是错误的。能否请您指导我如何创建double of Company
可供Employee
其关联的对象?
我没有使用FactoryGirl
。
非常感谢。