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_its_valueConvenientService::RSpec::Matchers::Classes::CallChainNext



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

def and_return_its_value
  chain[:and_return_its_value] = true

  self
end

#descriptionString

Returns:

  • (String)


104
105
106
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 104

def description
  "call `chain.next`"
end

#does_not_match?(block_expectation) ⇒ Bool

Parameters:

  • block_expectation (Proc, nil)

Returns:

  • (Bool)

Raises:

  • (RSpec::Expectations::ExpectationNotMetError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 63

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

  true
end

#failure_messageString

Returns:

  • (String)


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

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

#failure_message_when_negatedString

Returns:

  • (String)


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

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)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 31

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

  true
end

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

Parameters:

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

Returns:



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

def on(method)
  method.reset!

  chain[:method] = method

  self
end

#supports_block_expectations?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/convenient_service/rspec/matchers/classes/call_chain_next.rb', line 97

def supports_block_expectations?
  true
end

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



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

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:



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

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

  self
end

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



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

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

  self
end