From 07d8f226127b30e6a0b05d13815ba5d4d9cdccf0 Mon Sep 17 00:00:00 2001 From: Yosuke Kabuto Date: Sun, 29 May 2016 19:51:36 +0900 Subject: [PATCH 1/3] Remove white spaces --- lib/google/apis/errors.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/google/apis/errors.rb b/lib/google/apis/errors.rb index 96c5a4056..29fed01de 100644 --- a/lib/google/apis/errors.rb +++ b/lib/google/apis/errors.rb @@ -19,7 +19,7 @@ module Google attr_reader :status_code attr_reader :header attr_reader :body - + def initialize(err, status_code: nil, header: nil, body: nil) @cause = nil @@ -42,7 +42,7 @@ module Google end end end - + # An error which is raised when there is an unexpected response or other # transport error that prevents an operation from succeeding. class TransmissionError < Error From 657bfc0e01f87574fefbb347773811562e431dcd Mon Sep 17 00:00:00 2001 From: Yosuke Kabuto Date: Sun, 29 May 2016 19:52:27 +0900 Subject: [PATCH 2/3] Create specs for Google::Apis::Error --- spec/google/apis/errors_spec.rb | 96 +++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 spec/google/apis/errors_spec.rb diff --git a/spec/google/apis/errors_spec.rb b/spec/google/apis/errors_spec.rb new file mode 100644 index 000000000..4585679e8 --- /dev/null +++ b/spec/google/apis/errors_spec.rb @@ -0,0 +1,96 @@ +# Copyright 2016 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require 'spec_helper' + +RSpec.describe Google::Apis::Error do + describe 'constructor' do + subject { described_class.new(error) } + + describe 'message' do + context 'first arg is a string which does not respond to :backtrace' do + let(:error) { 'an error occurred' } + + it 'creates a instance of Error with message' do + expect(subject.message).to eq 'an error occurred' + end + end + + context 'first arg responds to :backtrace' do + let(:error) { double } + + it 'creates a instance of Error with message' do + allow(error).to receive(:respond_to?).with(:backtrace) { true } + allow(error).to receive(:message) { 'error message' } + expect(subject.message).to eq 'error message' + end + end + end + + describe 'instance variables @body, @header, and @status_code' do + let(:error) { 'an error occurred' } + + context 'params :body, :header, and :status_code are not given' do + it 'instance variables @body, @header, and @status_code will be nil' do + expect(subject.body).to be_nil + expect(subject.header).to be_nil + expect(subject.status_code).to be_nil + end + end + + context 'params :body, :header, and :status_code are given' do + subject do + described_class.new( + error, + body: 'this is body', + header: 'this is header', + status_code: 200) + end + + it 'instance variables @body, @header, and @status_code will not be nil' do + expect(subject.body).to eq 'this is body' + expect(subject.header).to eq 'this is header' + expect(subject.status_code).to eq 200 + end + end + end + end + + describe '#backtrace' do + subject { described_class.new('error') } + let(:cause) { instance_double('Error', backtrace: 'this is backtrace') } + + context '@cause is truthy' do + it 'calls @cause.backtrace' do + subject.instance_variable_set(:@cause, cause) + expect(subject.backtrace).to eq 'this is backtrace' + end + end + + context '@cause is falsy' do + before do + subject.class.superclass.class_eval do + def backtrace + "super class's #backtrace called" + end + end + end + + it "calls super class's #backtrace" do + subject.instance_variable_set(:@cause, false) + expect(subject.backtrace).to eq "super class's #backtrace called" + end + end + end +end From ab8cfe52081e8a02366746d7f27cc377d29c9cfb Mon Sep 17 00:00:00 2001 From: Yosuke Kabuto Date: Sun, 29 May 2016 20:20:59 +0900 Subject: [PATCH 3/3] Stub instance method :backtrace, insted of defining it --- spec/google/apis/errors_spec.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spec/google/apis/errors_spec.rb b/spec/google/apis/errors_spec.rb index 4585679e8..ad2a576e3 100644 --- a/spec/google/apis/errors_spec.rb +++ b/spec/google/apis/errors_spec.rb @@ -80,10 +80,8 @@ RSpec.describe Google::Apis::Error do context '@cause is falsy' do before do - subject.class.superclass.class_eval do - def backtrace - "super class's #backtrace called" - end + subject.class.superclass.any_instance.stub(:backtrace) do + "super class's #backtrace called" end end