我的工厂有一个url字段(用于获取youtube嵌入式视频):
factory :commercial do
url 'https://www.youtube.com/watch?v=BTTygyxuGj8'
end在我的测试中,我试图嘲笑youtube上的请求。我在我的spec_helper中添加了以下内容
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)
RSpec.configure do |config|
config.before(:suite) do
mock_request
end
end
def mock_request
response = {
author_name: 'Confreaks',
# more attributes omitted ..
}
WebMock.stub_request(:get, /.*youtube.*/).
to_return(status: 200, body: response.to_json, headers: {})
end在rspec配置中注册一次test模拟不足以使存根在整个测试套件中可用吗?为什么我要这样对我的工厂:
factory :commercial do
url 'https://www.youtube.com/watch?v=BTTygyxuGj8'
after(:build) do |commercial|
mock_request
end
end如果没有after(:build),我将得到以下错误:
Failure/Error: create(:commercial,WebMock::NetConnectNot合金化Error:真正的HTTP连接被禁用)。未注册请求:...tells我关于未注册请求的youtube和我应该如何存根它..。
发布于 2016-02-22 02:23:14
在to_return call中填写标头有帮助吗?
.to_return(
...,
headers: { "Content-Type" => "application/json; charset=utf-8" }
)用失败输出提供的值填充哈希。
https://stackoverflow.com/questions/35535617
复制相似问题