-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Fretadao/bv-add-spec-support
Add RSpec support for mock and match results
- Loading branch information
Showing
12 changed files
with
188 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'rspec/support' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'support/helpers' | ||
require_relative 'support/matchers' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'helpers/result' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
# Methods to mock a FService result from a service call. | ||
module FServiceResultHelpers | ||
# Create an Fservice result Success or Failure. | ||
def f_service_result(result, value = nil, type = nil) | ||
if result == :success | ||
FService::Result::Success.new(value, type) | ||
else | ||
FService::Result::Failure.new(value, type) | ||
end | ||
end | ||
|
||
# Mock a Fservice service call returning a result. | ||
def mock_service(service, result: :success, value: nil, type: nil) | ||
service_result = f_service_result(result, value, type) | ||
allow(service).to receive(:call).and_return(service_result) | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.include FServiceResultHelpers | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'matchers/result' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rspec/expectations' | ||
|
||
RSpec::Matchers.define :have_failed_with do |expected| | ||
match do |actual| | ||
matched = actual.is_a?(FService::Result::Failure) && actual.type == expected | ||
|
||
matched &&= actual.error == @expected_error if defined?(@expected_error) | ||
|
||
matched | ||
end | ||
|
||
chain :and_error do |expected_error| | ||
@expected_error = expected_error | ||
end | ||
|
||
failure_message do |actual| | ||
if actual.is_a?(FService::Result::Failure) | ||
message = "expected failure's type '#{actual.type.inspect}' to be equal '#{expected.inspect}'" | ||
if defined?(@expected_error) | ||
has_description = @expected_error.respond_to?(:description) | ||
message += " and error '#{actual.error.inspect}' to be " | ||
message += has_description ? @expected_error.description : "equal '#{@expected_error.inspect}'" | ||
end | ||
|
||
message | ||
else | ||
"result '#{actual.inspect}' is not a Failure object" | ||
end | ||
end | ||
end | ||
|
||
RSpec::Matchers.define :have_succeed_with do |expected| | ||
match do |actual| | ||
matched = actual.is_a?(FService::Result::Success) && actual.type == expected | ||
|
||
matched &&= values_match?(@expected_value, actual.value) if defined?(@expected_value) | ||
|
||
matched | ||
end | ||
|
||
chain :and_value do |expected_value| | ||
@expected_value = expected_value | ||
end | ||
|
||
failure_message do |actual| | ||
if actual.is_a?(FService::Result::Success) | ||
message = "expected success's type '#{actual.type.inspect}' to be equal '#{expected.inspect}'" | ||
if defined?(@expected_value) | ||
has_description = @expected_value.respond_to?(:description) | ||
message += " and value '#{actual.value.inspect}' to be " | ||
message += has_description ? @expected_value.description : "equal '#{@expected_value.inspect}'" | ||
end | ||
|
||
message | ||
else | ||
"result '#{actual.inspect}' is not a Success object" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters