From 98688a58eec152154f4eb38267878ac526e3de46 Mon Sep 17 00:00:00 2001 From: John Hope Date: Wed, 28 Feb 2018 19:18:34 +0000 Subject: [PATCH 1/3] Remove specific error check Rspec warns that checks for specific errors are discouraged; as any other error will be ignored. This allows the test to pass even if the code is never actually run. This change removes the check for a specific error and checks for any error instead. --- spec/httparty/response_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/httparty/response_spec.rb b/spec/httparty/response_spec.rb index 845adcae..b9aff086 100644 --- a/spec/httparty/response_spec.rb +++ b/spec/httparty/response_spec.rb @@ -66,7 +66,7 @@ subject { described_class.new(request, @response_object, @parsed_response) } it 'does not throw exception' do - expect{ subject }.not_to raise_error(HTTParty::ResponseError) + expect{ subject }.not_to raise_error end end end From 60179afb4a7048687f0e4034e8b29190b8925345 Mon Sep 17 00:00:00 2001 From: John Hope Date: Wed, 28 Feb 2018 19:48:46 +0000 Subject: [PATCH 2/3] Instantiates variables used during test execution. Rspec complains when using non-instantiated instance variables in tests, even if they're not critical to the purpose of the test. This change ensures all instance variables used in, or executing during, tests have values. --- spec/httparty/connection_adapter_spec.rb | 9 ++++++--- spec/httparty/net_digest_auth_spec.rb | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/spec/httparty/connection_adapter_spec.rb b/spec/httparty/connection_adapter_spec.rb index 9caf60a5..e88be5e3 100644 --- a/spec/httparty/connection_adapter_spec.rb +++ b/spec/httparty/connection_adapter_spec.rb @@ -32,9 +32,12 @@ end describe ".call" do + let(:uri) { URI 'http://www.google.com' } + let(:options) { { foo: :bar } } + it "generates an HTTParty::ConnectionAdapter instance with the given uri and options" do - expect(HTTParty::ConnectionAdapter).to receive(:new).with(@uri, @options).and_return(double(connection: nil)) - HTTParty::ConnectionAdapter.call(@uri, @options) + expect(HTTParty::ConnectionAdapter).to receive(:new).with(uri, options).and_return(double(connection: nil)) + HTTParty::ConnectionAdapter.call(uri, options) end it "calls #connection on the connection adapter" do @@ -42,7 +45,7 @@ connection = double('Connection') expect(adapter).to receive(:connection).and_return(connection) allow(HTTParty::ConnectionAdapter).to receive_messages(new: adapter) - expect(HTTParty::ConnectionAdapter.call(@uri, @options)).to be connection + expect(HTTParty::ConnectionAdapter.call(uri, options)).to be connection end end diff --git a/spec/httparty/net_digest_auth_spec.rb b/spec/httparty/net_digest_auth_spec.rb index 14bbadff..fb1a3683 100644 --- a/spec/httparty/net_digest_auth_spec.rb +++ b/spec/httparty/net_digest_auth_spec.rb @@ -23,6 +23,8 @@ def cookie_header include Net::HTTPHeader def initialize @header = {} + @path = '/' + @method = 'GET' end end).new } From 2291d553a633f7215d26a986da37e916a52aa4cf Mon Sep 17 00:00:00 2001 From: John Hope Date: Wed, 28 Feb 2018 20:56:44 +0000 Subject: [PATCH 3/3] Avoid redefining classes between tests. Defining classes within tests pollutes other namespaces, causing warnings that classes are being redefined when running tests. This change creates anonymous classes for use within individual tests. --- spec/httparty/parser_spec.rb | 8 ++++---- spec/httparty/request_spec.rb | 2 +- spec/httparty_spec.rb | 9 +++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/spec/httparty/parser_spec.rb b/spec/httparty/parser_spec.rb index ec207679..fea816ae 100644 --- a/spec/httparty/parser_spec.rb +++ b/spec/httparty/parser_spec.rb @@ -27,10 +27,10 @@ end it "returns the SupportedFormats constant for subclasses" do - class MyParser < HTTParty::Parser - SupportedFormats = {"application/atom+xml" => :atom} - end - expect(MyParser.formats).to eq({"application/atom+xml" => :atom}) + klass = Class.new(HTTParty::Parser) + klass::SupportedFormats = { "application/atom+xml" => :atom } + + expect(klass.formats).to eq({"application/atom+xml" => :atom}) end end diff --git a/spec/httparty/request_spec.rb b/spec/httparty/request_spec.rb index 2e31d877..63bf6fcf 100644 --- a/spec/httparty/request_spec.rb +++ b/spec/httparty/request_spec.rb @@ -591,7 +591,7 @@ stub_request(:get, 'http://api.foo.com/v2') .to_return(body: 'bar') body = "" - response = @request.perform { |chunk| body += chunk } + @request.perform { |chunk| body += chunk } expect(body.length).to eq(27) end diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index c27703f6..09e9d403 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -358,11 +358,12 @@ def self.parse(body) it "raises UnsupportedFormat when the parser cannot handle the format" do @klass.format :json - class MyParser < HTTParty::Parser - SupportedFormats = {} - end unless defined?(MyParser) + + parser_class = Class.new(HTTParty::Parser) + parser_class::SupportedFormats = {} + expect do - @klass.parser MyParser + @klass.parser parser_class end.to raise_error(HTTParty::UnsupportedFormat) end