Class: ConvenientService::RSpec::Matchers::Classes::CallChainNext

Inherits:
Object
  • Object
show all
Includes:
RSpec::Expectations, RSpec::Matchers, RSpec::Mocks::ExampleMethods
Defined in:
lib/convenient_service/rspec/matchers/classes/call_chain_next.rb

Overview

Examples:

Common usage.

expect { method.call }
  .to call_chain_next.on(method)
  .with_arguments(*args, **kwargs, &block)
  .and_return_its_value

Instance Method Summary collapse

Instance Method Details

#and_return(*args, &block) ⇒ ConvenientService::RSpec::Matchers::Classes::CallChainNext

Parameters:

  • args (Array<Object>)
  • block (Proc, nil)

Returns:



199
200
201
202
203
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 199

def and_return(*args, &block)
  chain[:and_return] = args.any? ? proc { args.first } : block

  self
end

#and_return_its_valueConvenientService::RSpec::Matchers::Classes::CallChainNext



187
188
189
190
191
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 187

def and_return_its_value
  chain[:and_return_its_value] = true

  self
end

#descriptionString

Returns:

  • (String)


124
125
126
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 124

def description
  "call `chain.next`"
end

#does_not_match?(block_expectation) ⇒ Bool

Parameters:

  • block_expectation (Proc, nil)

Returns:

  • (Bool)

Raises:

  • (RSpec::Expectations::ExpectationNotMetError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 79

def does_not_match?(block_expectation)
  @block_expectation = block_expectation

  value = nil

  ##
  # NOTE: `expect { value = block_expectation.call }.not_to change(method, :chain_called?).from(false).to(true)` raises the following error:
  #
  #   NotImplementedError:
  #     `expect { }.not_to change { }.to()` is not supported
  #
  # That is why `expect(method.chain_called?).to eq(false)` is introduced.
  # - https://github.com/rspec/rspec-expectations/blob/main/lib/rspec/matchers/built_in/change.rb#L50
  #
  expect(method.chain_called?).to eq(false)

  expect { value = block_expectation.call }.not_to change(method, :chain_called?)

  if used_with_arguments?
    expect(method.chain_args).not_to eq(expected_args)
    expect(method.chain_kwargs).not_to eq(expected_kwargs)
    expect(method.chain_block).not_to eq(expected_block)
  end

  if used_and_return_its_value?
    expect(value).not_to eq(method.chain_value)
  end

  if used_and_return?
    expect(value).not_to eq(return_block.call(method.chain_value))
  end

  true
end

#failure_messageString

Returns:

  • (String)


131
132
133
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 131

def failure_message
  "expected to call `chain.next`"
end

#failure_message_when_negatedString

Returns:

  • (String)


138
139
140
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 138

def failure_message_when_negated
  "expected NOT to call `chain.next`"
end

#matches?(block_expectation) ⇒ Bool

Parameters:

  • block_expectation (Proc, nil)

Returns:

  • (Bool)

Raises:

  • (RSpec::Expectations::ExpectationNotMetError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 43

def matches?(block_expectation)
  @block_expectation = block_expectation

  value = nil

  expect { value = block_expectation.call }.to change(method, :chain_called?).from(false).to(true)

  if used_with_arguments?
    expect(method.chain_args).to eq(expected_args)
    expect(method.chain_kwargs).to eq(expected_kwargs)
    expect(method.chain_block).to eq(expected_block)
  end

  if used_and_return_its_value?
    expect(value).to eq(method.chain_value)
  end

  if used_and_return?
    expect(value).to eq(return_block.call(method.chain_value))
  end

  true
end

#on(method) ⇒ ConvenientService::RSpec::Matchers::Classes::CallChainNext

Parameters:

  • method (ConvenientService::RSpec::Helpers::Classes::Entities::WrappedMethod)

Returns:



146
147
148
149
150
151
152
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 146

def on(method)
  method.reset!

  chain[:method] = method

  self
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 117

def supports_block_expectations?
  true
end

#with_any_argumentsConvenientService::RSpec::Matchers::Classes::CallChainNext



169
170
171
172
173
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 169

def with_any_arguments
  chain.delete(:with_arguments)

  self
end

#with_arguments(*args, **kwargs, &block) ⇒ ConvenientService::RSpec::Matchers::Classes::CallChainNext

Parameters:

  • args (Array<Object>)
  • kwargs (Hash{Symbol => Object})
  • block (Proc, nil)

Returns:



160
161
162
163
164
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 160

def with_arguments(*args, **kwargs, &block)
  chain[:with_arguments] = {args: args, kwargs: kwargs, block: block}

  self
end

#without_argumentsConvenientService::RSpec::Matchers::Classes::CallChainNext



178
179
180
181
182
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 178

def without_arguments
  chain[:with_arguments] = {args: [], kwargs: {}, block: nil}

  self
end