From c1f90db7c1d49c0289da5edb170ae65342c8e8f7 Mon Sep 17 00:00:00 2001 From: Ruby Date: Mon, 1 Apr 2019 22:31:24 +0700 Subject: [PATCH 1/2] Adding spec for error mapper --- bin/console | 1 + lib/jsonapi_errors_handler/error_mapper.rb | 2 +- spec/error_mapper_spec.rb | 35 ++++++++++++++++++++++ spec/jsonapi_errors_handler_spec.rb | 2 ++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 spec/error_mapper_spec.rb diff --git a/bin/console b/bin/console index c7d1ece..ec8458a 100755 --- a/bin/console +++ b/bin/console @@ -1,6 +1,7 @@ #!/usr/bin/env ruby require "bundler/setup" +require "rspec" require "jsonapi_errors_handler" # You can add fixtures and/or initialization code here to make experimenting diff --git a/lib/jsonapi_errors_handler/error_mapper.rb b/lib/jsonapi_errors_handler/error_mapper.rb index 486b5ac..a3fc5a8 100644 --- a/lib/jsonapi_errors_handler/error_mapper.rb +++ b/lib/jsonapi_errors_handler/error_mapper.rb @@ -1,11 +1,11 @@ module JsonapiErrorsHandler class ErrorMapper + @@mapped_errors = {} def self.mapped_errors @@mapped_errors end def self.map_errors!(errors_hash={}) - @@mapped_errors ||= {} @@mapped_errors.merge!(errors_hash) end diff --git a/spec/error_mapper_spec.rb b/spec/error_mapper_spec.rb new file mode 100644 index 0000000..d79ba2f --- /dev/null +++ b/spec/error_mapper_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +RSpec.describe JsonapiErrorsHandler::ErrorMapper do + let(:subject) { described_class } + + before(:each) { + subject.class_variable_set(:@@mapped_errors, {}) + } + + describe '.mapped_errors' do + it 'returns all the current mapped errors' do + expect(subject.mapped_errors).to eq({}) + end + end + + describe '.map_errors!' do + let(:errors_hash) { {'Error' => 'Invalid'} } + + it 'merges errors_hash into the current errors' do + result = subject.map_errors!(errors_hash) + expect(result['Error']).to eq 'Invalid' + end + end + + describe '.mapped_errors?' do + it 'return false if the errors does not include a specific error' do + expect(subject.mapped_error?('AnError')).to be false + end + + it 'return true if the errors include a specific error' do + subject.map_errors!({ 'Invalid' => 'Errors::Invalid'}) + expect(subject.mapped_error?('Errors::Invalid')).to be true + end + end +end diff --git a/spec/jsonapi_errors_handler_spec.rb b/spec/jsonapi_errors_handler_spec.rb index 7537fed..014b4a5 100644 --- a/spec/jsonapi_errors_handler_spec.rb +++ b/spec/jsonapi_errors_handler_spec.rb @@ -1,3 +1,5 @@ +require 'spec_helper' + RSpec.describe JsonapiErrorsHandler do it "has a version number" do expect(JsonapiErrorsHandler::VERSION).not_to be nil From 383d795a326a01b46df20e1226a34cbcc766da61 Mon Sep 17 00:00:00 2001 From: Ruby Date: Tue, 2 Apr 2019 22:28:59 +0700 Subject: [PATCH 2/2] Refactor test for error mapper --- bin/console | 1 - spec/error_mapper_spec.rb | 16 +++++----------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/bin/console b/bin/console index ec8458a..c7d1ece 100755 --- a/bin/console +++ b/bin/console @@ -1,7 +1,6 @@ #!/usr/bin/env ruby require "bundler/setup" -require "rspec" require "jsonapi_errors_handler" # You can add fixtures and/or initialization code here to make experimenting diff --git a/spec/error_mapper_spec.rb b/spec/error_mapper_spec.rb index d79ba2f..80b35ff 100644 --- a/spec/error_mapper_spec.rb +++ b/spec/error_mapper_spec.rb @@ -1,15 +1,9 @@ require 'spec_helper' RSpec.describe JsonapiErrorsHandler::ErrorMapper do - let(:subject) { described_class } - - before(:each) { - subject.class_variable_set(:@@mapped_errors, {}) - } - describe '.mapped_errors' do it 'returns all the current mapped errors' do - expect(subject.mapped_errors).to eq({}) + expect(described_class.mapped_errors).to eq({}) end end @@ -17,19 +11,19 @@ let(:errors_hash) { {'Error' => 'Invalid'} } it 'merges errors_hash into the current errors' do - result = subject.map_errors!(errors_hash) + result = described_class.map_errors!(errors_hash) expect(result['Error']).to eq 'Invalid' end end describe '.mapped_errors?' do it 'return false if the errors does not include a specific error' do - expect(subject.mapped_error?('AnError')).to be false + expect(described_class.mapped_error?('AnError')).to be false end it 'return true if the errors include a specific error' do - subject.map_errors!({ 'Invalid' => 'Errors::Invalid'}) - expect(subject.mapped_error?('Errors::Invalid')).to be true + described_class.map_errors!({ 'Invalid' => 'Errors::Invalid'}) + expect(described_class.mapped_error?('Errors::Invalid')).to be true end end end