Cuprum

An opinionated implementation of the Command pattern for Ruby applications. Cuprum wraps your business logic in a consistent, object-oriented interface and features status and error management, composability and control flow management.

Class: Cuprum::RSpec::BeAResultMatcher

Parent Namespace
Cuprum::RSpec
Defined In
lib/cuprum/rspec/be_a_result_matcher.rb

Table Of Contents

Overview

Asserts the actual object is a result object with the specified properties.

If initialized with a class, the matcher will assert that the actual object is an instance of that class. This can be useful for asserting that the result is an instance of a result subclass. If no class is given, the matcher asserts that the result is an object responding to #to_cuprum_result.

The matcher also defines fluent methods for asserting on the result’s properties:

Generally speaking, you should use the #be_a_result, #be_a_passing_result, and #be_a_failing_result macros, rather than instantiating a BeAResultMatcher directly.

Examples

Matching Any Result

# Or use expect().to be_a_result
matcher = Cuprum::RSpec::BeAResultMatcher.new

matcher.matches?(nil)                #=> false
matcher.matches?(Cuprum::Result.new) #=> true

Matching A Result Status

# Or use expect().to be_a_passing_result
matcher = Cuprum::RSpec::BeAResultMatcher.new.with_status(:success)

matcher.matches?(Cuprum::Result.new(status: :failure)) #=> false
matcher.matches?(Cuprum::Result.new(status: :success)) #=> false

Matching A Result Value

matcher = Cuprum::RSpec::BeAResultMatcher.new.with_value({ ok: true })

matcher.matches?(Cuprum::Result.new(value: { ok: false })) #=> false
matcher.matches?(Cuprum::Result.new(value: { ok: true }))  #=> true

Matching A Result Error

error   = Cuprum::Error.new(message: 'Something went wrong')
matcher = Cuprum::RSpec::BeAResultMatcher.new.with_error(error)

other_error = Cuprum::Error.new(message: 'Oh no')
matcher.matches?(Cuprum::Result.new(error: other_error) #=> false
matcher.matches?(Cuprum::Result.new(error: error)       #=> true

Matching A Result Class

matcher = Cuprum::RSpec::BeAResultMatcher.new(CustomResult)

matcher.matches?(Cuprum::Result.new) #=> false
matcher.matches?(CustomResult.new)   #=> true

Matching Multiple Properties

matcher =
  Cuprum::RSpec::BeAResultMatcher
  .with_status(:failure)
  .and_value({ ok: false })
  .and_error(Cuprum::Error.new(message: 'Something went wrong'))

Back To Top

Constructor

#initialize(expected_class = nil) => BeAResultMatcher

Parameters

Returns

Back To Top

Instance Attributes

#expected_class => Class (readonly)

Returns

Back To Top

Instance Methods

#description => String

Returns

#does_not_match?(actual) => Boolean

Checks that the given actual object is not a Cuprum result.

Parameters

Returns

Raises

#failure_message => String

Returns

#failure_message_when_negated => String

Returns

#matches?(actual) => Boolean

Checks that the given actual object is a Cuprum result or compatible object and has the specified properties.

Parameters

Returns

#with_error(error) => BeAResultMatcher

Also known as: and_error

Sets an error expectation on the matcher. Calls to #matches? will fail unless the actual object has the specified error.

Parameters

Returns

#with_status(status) => BeAResultMatcher

Also known as: and_status

Sets a status expectation on the matcher. Calls to #matches? will fail unless the actual object has the specified status.

Parameters

Returns

#with_value(value) => BeAResultMatcher

Also known as: and_value

Sets a value expectation on the matcher. Calls to #matches? will fail unless the actual object has the specified value.

Parameters

Returns

Back To Top


Back to Documentation | Reference | Cuprum | Cuprum::RSpec