Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用rspec测试应接收的方法_Ruby On Rails_Ruby_Ruby On Rails 3_Rspec_Rspec Rails - Fatal编程技术网

Ruby on rails 使用rspec测试应接收的方法

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

我有一个address方法,它实例化一个新类并返回一些信息

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>
}