如何在rspec中模拟对象及其属性

时间:2015-05-28 15:00:24

标签: ruby-on-rails-4 rspec rspec3

我有一个控制器操作,我试图测试下载文件。

it 'calls Net::HTTP.get_response which returns a response' do
    @netresponse = double("response", {:body => 'this is a test', :code => 200})
    @url = 'http://www.google.com'
    allow(Net::HTTP).to receive(:get_response).and_return(@netresponse)
    return_value = controller.download_file(@url)
    expect(return_value).to eq('this is a test')
end

在阅读rspec时,我发现我可以创建一个对象的双重并存根它的方法。但是,我似乎无法弄清楚如何嘲笑它的属性。因此,对于此测试,如何模拟从get_response调用返回的响应以获得代码和body属性?我试过了,但它没有用。相反,我没有回头。

{{1}}

1 个答案:

答案 0 :(得分:0)

您使用整数存储code,但实现与字符串进行比较。