Module: RSpec::Mocks::TestDouble

Included in:
Double, InstanceVerifyingDouble, ObjectVerifyingDoubleMethods
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb

Overview

Implements the methods needed for a pure test double. RSpec::Mocks::Double includes this module, and it is provided for cases where you want a pure test double without subclassing RSpec::Mocks::Double.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(message, *args, &block) ⇒ Object (private)

Raises:

  • (NoMethodError)


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
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb', line 74

def method_missing(message, *args, &block)
  proxy = __mock_proxy
  proxy.record_message_received(message, *args, &block)

  if proxy.null_object?
    case message
    when :to_int        then return 0
    when :to_a, :to_ary then return nil
    when :to_str        then return to_s
    else return self
    end
  end

  # Defined private and protected methods will still trigger `method_missing`
  # when called publicly. We want ruby's method visibility error to get raised,
  # so we simply delegate to `super` in that case.
  # ...well, we would delegate to `super`, but there's a JRuby
  # bug, so we raise our own visibility error instead:
  # https://github.com/jruby/jruby/issues/1398
  visibility = proxy.visibility_for(message)
  if visibility == :private || visibility == :protected
    ErrorGenerator.new(self).raise_non_public_error(
      message, visibility
    )
  end

  # Required wrapping doubles in an Array on Ruby 1.9.2
  raise NoMethodError if [:to_a, :to_ary].include? message
  proxy.raise_unexpected_message_error(message, args)
end

Instance Method Details

#==(other) ⇒ Object

This allows for comparing the mock to other objects that proxy such as ActiveRecords belongs_to proxy objects. By making the other object run the comparison, we’re sure the call gets delegated to the proxy target.



36
37
38
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb', line 36

def ==(other)
  other == __mock_proxy
end

#__build_mock_proxy_unless_expired(order_group) ⇒ Object



56
57
58
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb', line 56

def __build_mock_proxy_unless_expired(order_group)
  __raise_expired_error || __build_mock_proxy(order_group)
end

#__disallow_further_usage!Object



61
62
63
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb', line 61

def __disallow_further_usage!
  @__expired = true
end

#as_null_objectObject

Tells the object to respond to all messages. If specific stub values are declared, they’ll work as expected. If not, the receiver is returned.



23
24
25
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb', line 23

def as_null_object
  __mock_proxy.as_null_object
end

#freezeObject

Override for default freeze implementation to prevent freezing of test doubles.



67
68
69
70
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb', line 67

def freeze
  RSpec.warn_with("WARNING: you attempted to freeze a test double. This is explicitly a no-op as freezing doubles can lead to undesired behaviour when resetting tests.")
  self
end

#initialize(name = nil, stubs = {}) ⇒ Object

Creates a new test double with a ‘name` (that will be used in error messages only)



9
10
11
12
13
14
15
16
17
18
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb', line 9

def initialize(name=nil, stubs={})
  @__expired = false
  if Hash === name && stubs.empty?
    stubs = name
    @name = nil
  else
    @name = name
  end
  assign_stubs(stubs)
end

#inspectObject



41
42
43
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb', line 41

def inspect
  TestDoubleFormatter.format(self)
end

#null_object?Boolean

Returns true if this object has received ‘as_null_object`

Returns:

  • (Boolean)


28
29
30
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb', line 28

def null_object?
  __mock_proxy.null_object?
end

#respond_to?(message, incl_private = false) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb', line 51

def respond_to?(message, incl_private=false)
  __mock_proxy.null_object? ? true : super
end

#to_sObject



46
47
48
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/test_double.rb', line 46

def to_s
  inspect.tr('<', '[').tr('>', ']')
end