Skip to content

Commit

Permalink
Adds error tests & method to get response object from Error class
Browse files Browse the repository at this point in the history
  • Loading branch information
cakejelly committed Jul 21, 2015
1 parent ad09367 commit 55f2f10
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/layer/api/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def initialize(response)
super(build_error_message)
end

def response
@response
end

private

def build_error_message
Expand Down
2 changes: 1 addition & 1 deletion lib/layer/api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Layer
module Api
VERSION = "0.1.1"
VERSION = "0.2.0"
end
end
59 changes: 59 additions & 0 deletions spec/layer/error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'

describe Layer::Api::Error do
describe ".from_response" do
it 'should raise NotFound error when status is 404' do
VCR.use_cassette('conversation') do
layer = Layer::Api::Client.new

expect {
layer.get_conversation("notfound")
}.to raise_error(Layer::Api::NotFound)
end
end

it "should raise BadRequest error when status is 400" do
response = {status: 400}
expect {
raise Layer::Api::Error.from_response(response)
}.to raise_error(Layer::Api::BadRequest)
end

(500..599).each do |error|
it "should raise ServerError when status is #{error}" do
response = {status: error}
expect {
raise Layer::Api::Error.from_response(response)
}.to raise_error(Layer::Api::ServerError)
end
end

it "should return Error when status isn't found/handled" do
expect {
raise Layer::Api::Error.from_response({})
}.to raise_error(Layer::Api::Error)
end
end

describe ".initialize" do
it "should set @response instance variable" do
begin
raise Layer::Api::Error.new({})
rescue => e
expect(e.response).to eq({})
end
end

it "should build correct error message containing status & body" do
status = '200'
body = 'random_test_body'
args = {
status: status,
body: body
}
error_msg = Layer::Api::Error.new(args).send(:build_error_message)
expect(error_msg).to include(status)
expect(error_msg).to include(body)
end
end
end

0 comments on commit 55f2f10

Please sign in to comment.