Net::http::Proxy(PROXY_HOST,PROXY_PORT).start(url.host) do |http| request = Net::http::Post.new(url.path) request.form_data = {'param1' => 'blah1','param2' => 'blah2'} response = http.request(request)end
这是我得到的……
@mock_http = mock('http')@mock_http.should_receive(:start).with(@url.host)Net::http.should_receive(:Proxy).with(PROXY_HOST,PROXY_PORT).and_return(@mock_http)Net::http::Post.should_receive(:new).with(@url.path).and_return(@mock_http)
然而,当尝试…
Net::http::Post.should_receive(:new).with(@url.path).and_return(@mock_http)
……收到以下回复……
<Net::http::Post (class)> expected :new with ("/some/path") once,but received it 0 times
一个完整的解决方案将不胜感激!
解决方法 我不确定你要测试的是什么,但是这里是如何使用webmock存根这个http请求:在Gemfile中:
group :test do gem 'webmock'end
在spec / spec_helper.rb中:
require 'webmock/rspec'WebMock.disable_net_connect!(:allow_localhost => true)
在你的测试中:
stub_request(:post,url.host). with(:body => {'param1' => 'blah1','param2' => 'blah2'},to_return(:status => 200,:body => '{ insert your response body expectation here }'总结
以上是内存溢出为你收集整理的ruby-on-rails – RSpec麻烦模拟HTTP请求全部内容,希望文章能够帮你解决ruby-on-rails – RSpec麻烦模拟HTTP请求所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)