From 8408ffb25c9104093097a6e401f51eaff5555fd7 Mon Sep 17 00:00:00 2001 From: Andy Brody Date: Wed, 3 May 2017 10:47:59 -0400 Subject: [PATCH] Helpers: switch to verifying instance_double. --- spec/helpers.rb | 24 ++++++++++++++++++++---- spec/unit/abstract_response_spec.rb | 14 +++++++------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/spec/helpers.rb b/spec/helpers.rb index e23faaf..8a25a50 100644 --- a/spec/helpers.rb +++ b/spec/helpers.rb @@ -1,10 +1,23 @@ require 'uri' module Helpers + + # @param [Hash] opts A hash of methods, passed directly to the double + # definition. Use this to stub other required methods. + # + # @return double for Net::HTTPResponse def res_double(opts={}) - double('Net::HTTPResponse', {to_hash: {}, body: 'response body'}.merge(opts)) + instance_double('Net::HTTPResponse', {to_hash: {}, body: 'response body'}.merge(opts)) end + # Given a Net::HTTPResponse or double and a Request or double, create a + # RestClient::Response object. + # + # @param net_http_res_double an rspec double for Net::HTTPResponse + # @param request A RestClient::Request or rspec double + # + # @return [RestClient::Response] + # def response_from_res_double(net_http_res_double, request=nil, duration: 1) request ||= request_double() start_time = Time.now - duration @@ -17,6 +30,7 @@ module Helpers response end + # Redirect stderr to a string for the duration of the passed block. def fake_stderr original_stderr = $stderr $stderr = StringIO.new @@ -26,9 +40,11 @@ module Helpers $stderr = original_stderr end + # Create a double for RestClient::Request def request_double(url: 'http://example.com', method: 'get') - double('request', url: url, uri: URI.parse(url), method: method, - user: nil, password: nil, cookie_jar: HTTP::CookieJar.new, - redirection_history: nil, args: {url: url, method: method}) + instance_double('RestClient::Request', + url: url, uri: URI.parse(url), method: method, user: nil, password: nil, + cookie_jar: HTTP::CookieJar.new, redirection_history: nil, + args: {url: url, method: method}) end end diff --git a/spec/unit/abstract_response_spec.rb b/spec/unit/abstract_response_spec.rb index 0e221e6..edf6923 100644 --- a/spec/unit/abstract_response_spec.rb +++ b/spec/unit/abstract_response_spec.rb @@ -2,21 +2,21 @@ require_relative '_lib' describe RestClient::AbstractResponse, :include_helpers do + # Sample class implementing AbstractResponse used for testing. class MyAbstractResponse include RestClient::AbstractResponse attr_accessor :size - def initialize net_http_res, request - @net_http_res = net_http_res - @request = request + def initialize(net_http_res, request) + response_set_vars(net_http_res, request, Time.now - 1) end end before do - @net_http_res = double('net http response') + @net_http_res = res_double() @request = request_double(url: 'http://example.com', method: 'get') @response = MyAbstractResponse.new(@net_http_res, @request) end @@ -92,8 +92,8 @@ describe RestClient::AbstractResponse, :include_helpers do it 'handles cookies when URI scheme is implicit' do net_http_res = double('net http response') expect(net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/']) - request = double(url: 'example.com', uri: URI.parse('http://example.com'), - method: 'get', cookie_jar: HTTP::CookieJar.new) + request = double('request', url: 'example.com', uri: URI.parse('http://example.com'), + method: 'get', cookie_jar: HTTP::CookieJar.new, redirection_history: nil) response = MyAbstractResponse.new(net_http_res, request) expect(response.cookie_jar).to be_a HTTP::CookieJar @@ -135,7 +135,7 @@ describe RestClient::AbstractResponse, :include_helpers do end it "should gracefully handle 302 redirect with no location header" do - @net_http_res = res_double(code: 302, location: nil) + @net_http_res = res_double(code: 302) @request = request_double() @response = MyAbstractResponse.new(@net_http_res, @request) expect(@response).to receive(:check_max_redirects).and_return('fake-check')