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:



194
195
196
197
198
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 194

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

  self
end

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



182
183
184
185
186
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 182

def and_return_its_value
  chain[:and_return_its_value] = true

  self
end

#descriptionString

Returns:

  • (String)


119
120
121
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 119

def description
  "call `chain.next`"
end

#does_not_match?(block_expectation) ⇒ Bool

Parameters:

  • block_expectation (Proc, nil)

Returns:

  • (Bool)

Raises:

  • (RSpec::Expectations::ExpectationNotMetError)


74
75
76
77
78
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
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 74

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)


126
127
128
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 126

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

#failure_message_when_negatedString

Returns:

  • (String)


133
134
135
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 133

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)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 38

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:



141
142
143
144
145
146
147
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 141

def on(method)
  method.reset!

  chain[:method] = method

  self
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 112

def supports_block_expectations?
  true
end

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



164
165
166
167
168
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 164

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:



155
156
157
158
159
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 155

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

  self
end

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



173
174
175
176
177
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 173

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

  self
end