Class: MockProxy

Inherits:
Object show all
Defined in:
lib/mspec/mocks/proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(type = nil) ⇒ MockProxy

Returns a new instance of MockProxy.



13
14
15
16
17
18
19
# File 'lib/mspec/mocks/proxy.rb', line 13

def initialize(type=nil)
  @multiple_returns = nil
  @returning = nil
  @yielding  = []
  @arguments = :any_args
  @type      = type || :mock
end

Instance Method Details

#and_return(*args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mspec/mocks/proxy.rb', line 97

def and_return(*args)
  case args.size
  when 0
    @returning = nil
  when 1
    @returning = args[0]
  else
    @multiple_returns = true
    @returning = args
    count[1] = args.size if count[1] < args.size
  end
  self
end

#and_yield(*args) ⇒ Object



111
112
113
114
# File 'lib/mspec/mocks/proxy.rb', line 111

def and_yield(*args)
  @yielding << args
  self
end

#any_number_of_timesObject



83
84
85
86
# File 'lib/mspec/mocks/proxy.rb', line 83

def any_number_of_times
  @count = [:any_number_of_times, 0]
  self
end

#argumentsObject



33
34
35
# File 'lib/mspec/mocks/proxy.rb', line 33

def arguments
  @arguments
end

#at_least(n) ⇒ Object



65
66
67
68
# File 'lib/mspec/mocks/proxy.rb', line 65

def at_least(n)
  @count = [:at_least, n_times(n)]
  self
end

#at_most(n) ⇒ Object



70
71
72
73
# File 'lib/mspec/mocks/proxy.rb', line 70

def at_most(n)
  @count = [:at_most, n_times(n)]
  self
end

#calledObject



56
57
58
# File 'lib/mspec/mocks/proxy.rb', line 56

def called
  @calls = calls + 1
end

#callsObject



52
53
54
# File 'lib/mspec/mocks/proxy.rb', line 52

def calls
  @calls ||= 0
end

#countObject



29
30
31
# File 'lib/mspec/mocks/proxy.rb', line 29

def count
  @count ||= mock? ? [:exactly, 1] : [:any_number_of_times, 0]
end

#exactly(n) ⇒ Object



60
61
62
63
# File 'lib/mspec/mocks/proxy.rb', line 60

def exactly(n)
  @count = [:exactly, n_times(n)]
  self
end

#mock?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/mspec/mocks/proxy.rb', line 21

def mock?
  @type == :mock
end

#onceObject



75
76
77
# File 'lib/mspec/mocks/proxy.rb', line 75

def once
  exactly 1
end

#returningObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/mspec/mocks/proxy.rb', line 37

def returning
  if @multiple_returns
    if @returning.size == 1
      @multiple_returns = false
      return @returning = @returning.shift
    end
    return @returning.shift
  end
  @returning
end

#stub?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/mspec/mocks/proxy.rb', line 25

def stub?
  @type == :stub
end

#timesObject



48
49
50
# File 'lib/mspec/mocks/proxy.rb', line 48

def times
  self
end

#twiceObject



79
80
81
# File 'lib/mspec/mocks/proxy.rb', line 79

def twice
  exactly 2
end

#with(*args) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
# File 'lib/mspec/mocks/proxy.rb', line 88

def with(*args)
  raise ArgumentError, "you must specify the expected arguments" if args.empty?
  @arguments = *args
  if RUBY_VERSION >= '1.9'
    @arguments = @arguments.first if @arguments.length <= 1
  end
  self
end

#yieldingObject



116
117
118
# File 'lib/mspec/mocks/proxy.rb', line 116

def yielding
  @yielding
end

#yielding?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/mspec/mocks/proxy.rb', line 120

def yielding?
  !@yielding.empty?
end