在我的rails项目中,其中一个初始化器请求并从S3获取某些数据。
S3.buckets[CONFIG['aws']['cdn_bucket']].objects['object_name'].read这破坏了使用webmock gem的rspec测试套件。
WebMock.allow_net_connect!(:net_http_connect_on_start => true)当我试图运行测试套件时,我会得到以下错误
WebMock::NetConnectNotAllowedError
You can stub this request with the following snippet:
stub_request(:get, "https://bucket.s3.amazonaws.com/object_name").with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'', 'Authorization'=>'AWS AKxxxxxx:Hyxxxxxxxxxx', 'Content-Type'=>'', 'Date'=>'Thu, 14 Apr 2016 15:10:18 GMT', 'User-Agent'=>'aws-sdk-ruby/1.60.2 ruby/1.8.7 i686-darwin15.3.0'}).to_return(:status => 200, :body => "", :headers => {})添加此存根不会修复错误。事实上,添加以下任何一项似乎都不会做任何更改:
WebMock.stub_request(:any, /.*amazonaws.*/).with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'', 'Authorization'=>'AWS AKIxxxxxxxxxx:MSxxxxxxxx'}).to_return(:status => 200, :body => "stubbed response", :headers => {})WebMock.stub_request(:any, /.*amazonaws.*/).to_return(:status => 200, :body => "stubbed response", :headers => {})我在这里错过了什么?在这里,错误消息中的详细标题似乎没有意义允许对S3的各种请求。
编辑:
我刚刚注意到,将WebMock.disable!添加到spec_helper也不会导致任何更改。我不是把存根加到正确的地方了吗?如果不在spec_helper中,应该在哪里添加它?
发布于 2016-04-15 07:25:46
在睡觉之后,很明显,stub_request被添加到了错误的位置。直接将其添加到初始化程序可以修复这个问题,但这会破坏所有其他环境,因为gem webmock仅用于测试env。
因此,将下面的代码片段添加到这个脚本中
begin
require 'webmock'
WebMock.stub_request(:any, /testimonial/).to_return(:body => '')
rescue LoadError
end
S3.buckets[CONFIG['aws']['cdn_bucket']].objects['object_name'].read如果gem包括在内,这就产生了一个stub_request,否则,什么都不会发生,只会继续下去。
https://stackoverflow.com/questions/36627434
复制相似问题