Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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_Http_Rspec - Fatal编程技术网

Ruby on rails 在Rspec中,如何下载内容

Ruby on rails 在Rspec中,如何下载内容,ruby-on-rails,ruby,http,rspec,Ruby On Rails,Ruby,Http,Rspec,我想在rspec测试期间从网站下载 我不想存根这个特定函数的API用法。 我不打算对API进行大量测试。为此,我希望我能相信存根。 但我认为对一个API至少进行一次测试是个好主意 下面是一些复制我遇到的问题的示例代码: require 'vcr' context 'test vcr off' do it 'should work' do VCR.turn_off! res = Net::HTTP.get_response(URI('http://www.google.com.

我想在rspec测试期间从网站下载

我不想存根这个特定函数的API用法。 我不打算对API进行大量测试。为此,我希望我能相信存根。 但我认为对一个API至少进行一次测试是个好主意

下面是一些复制我遇到的问题的示例代码:

require 'vcr'
context 'test vcr off' do
  it 'should work' do
    VCR.turn_off!
    res = Net::HTTP.get_response(URI('http://www.google.com.au/?q=tester'))
    print res.body
  end
end
下面是我得到的错误:

Failure/Error: res = Net::HTTP.get_response(URI('http://www.google.com.au/?q=tester'))
     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET http://www.google.com.au/?q=tester with headers 
       {'Accept'=>'*/*', 
        'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
        'Host'=>'www.google.com.au', 'User-Agent'=>'Ruby'}

       You can stub this request with the following snippet:

       stub_request(:get, "http://www.google.com.au/?q=tester").
         with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Host'=>'www.google.com.au', 'User-Agent'=>'Ruby'}).
         to_return(:status => 200, :body => "", :headers => {})

我在录像机上读到的文件表明上述代码应该有效。

我真的很讨厌这种情况发生。。 我搜索一个又一个答案,然后我发布一个问题。 几分钟后我找到了答案

1 require 'vcr'
2 context 'test vcr off' do
3   it 'should work' do
4     VCR.turn_off!
5     WebMock.disable_net_connect!(:allow => "www.google.com.au")
6     res = Net::HTTP.get_response(URI('http://www.google.com.au/?q=tester'))
7     print res.body
8   end
9 end

答案在第5行。。这让我来打电话。。我的意思是我在屏幕上看到一堆垃圾,但它不会失败。。。也不例外

您可以从spec_helper.rb共享VCR配置吗?你用什么版本的录像机?