Ruby on rails 使用rspec测试应接收的方法
我有一个address方法,它实例化一个新类并返回一些信息Ruby on rails 使用rspec测试应接收的方法,ruby-on-rails,ruby,ruby-on-rails-3,rspec,rspec-rails,Ruby On Rails,Ruby,Ruby On Rails 3,Rspec,Rspec Rails,我有一个address方法,它实例化一个新类并返回一些信息 class Office attribute :id def address info = Services::Employee.new info.contact = [contact] info.id = id info end end 在上面的address方法中,contact(数组中的一个)是另一个方法,它返回一堆东西 现在在我的rspec测试中,我只想说info.contac
class Office
attribute :id
def address
info = Services::Employee.new
info.contact = [contact]
info.id = id
info
end
end
在上面的address方法中,contact(数组中的一个)是另一个方法,它返回一堆东西
现在在我的rspec测试中,我只想说info.contact返回[contact],而没有实际使用数据测试它。
我读过关于should_receive的文章,但我不确定如何使用should_receive测试上述方法
以下是我尝试过的:
info = Employee.deserialize(JSON.parse(info.to_json))
it 'returns info' do
Employee.should_receive(:info.contact).and_return('[contact]')
expect(helper.address).to eq(info)
end
我得到了这个错误:
expected: 1 time with any arguments
received: 0 times with any arguments
看起来
info
是测试中Employee
的一个实例。因此,是员工
接收消息联系人
尝试更改您的测试,以便将您应接收的直接放置在员工上
:
Employee.any_instance.should_receive(:contact).and_return('[contact]')
Services::Employee.。任何_实例。是否应_接收(:contact=){124;值|
value.should eq
}
contact
被作为实例方法调用,但您建议对类方法设置期望值。
Services::Employee,.any_instance.should_receive(:contact=) {|value|
value.should eq <expected_value>
<expected_value>
}