Class: Crspec::Mock::HaveReceivedMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/crspec/mock/double.rb

Overview

expect(target).to have_received(:method).with(...).once

Instance Method Summary collapse

Constructor Details

#initialize(method_name) ⇒ HaveReceivedMatcher

Returns a new instance of HaveReceivedMatcher.



361
362
363
364
365
366
367
# File 'lib/crspec/mock/double.rb', line 361

def initialize(method_name)
  @method_name = method_name.to_sym
  @expected_args = nil
  @expected_kwargs = nil
  @expected_count = nil
  @count_constraint = :at_least_once
end

Instance Method Details

#at_least(n) ⇒ Object



397
398
399
400
401
# File 'lib/crspec/mock/double.rb', line 397

def at_least(n)
  @expected_count = n
  @count_constraint = :at_least
  self
end

#exactly(n) ⇒ Object



387
388
389
390
391
# File 'lib/crspec/mock/double.rb', line 387

def exactly(n)
  @expected_count = n
  @count_constraint = :exact
  self
end

#failure_messageObject



421
422
423
424
425
426
427
428
429
430
# File 'lib/crspec/mock/double.rb', line 421

def failure_message
  expectation = case @count_constraint
                when :exact then "exactly #{@expected_count} time(s)"
                when :at_least then "at least #{@expected_count} time(s)"
                else "at least once"
                end
  with_clause = @expected_args ? " with #{@expected_args.inspect}" : ""
  "Expected #{@target.inspect} to have received #{@method_name.inspect}#{with_clause} #{expectation}, " \
    "but received it #{@actual_count} time(s)"
end

#failure_message_when_negatedObject



432
433
434
435
# File 'lib/crspec/mock/double.rb', line 432

def failure_message_when_negated
  "Expected #{@target.inspect} not to have received #{@method_name.inspect}, " \
    "but received it #{@actual_count} time(s)"
end

#matches?(target) ⇒ Boolean

Returns:

  • (Boolean)


403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/crspec/mock/double.rb', line 403

def matches?(target)
  @target = target
  calls = Space.current.calls_for(target, @method_name)
  if @expected_args || @expected_kwargs
    calls = calls.select do |args, kwargs|
      ArgumentMatchers.args_match?(@expected_args, args, kwargs) &&
        ArgumentMatchers.kwargs_match?(@expected_kwargs, kwargs)
    end
  end
  @actual_count = calls.size

  case @count_constraint
  when :exact then @actual_count == @expected_count
  when :at_least then @actual_count >= @expected_count
  else @actual_count.positive?
  end
end

#onceObject



375
376
377
378
379
# File 'lib/crspec/mock/double.rb', line 375

def once
  @expected_count = 1
  @count_constraint = :exact
  self
end

#timesObject



393
394
395
# File 'lib/crspec/mock/double.rb', line 393

def times
  self
end

#twiceObject



381
382
383
384
385
# File 'lib/crspec/mock/double.rb', line 381

def twice
  @expected_count = 2
  @count_constraint = :exact
  self
end

#with(*args, **kwargs) ⇒ Object



369
370
371
372
373
# File 'lib/crspec/mock/double.rb', line 369

def with(*args, **kwargs)
  @expected_args = args
  @expected_kwargs = kwargs
  self
end