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.



27
28
29
30
31
32
33
# File 'lib/mspec/mocks/proxy.rb', line 27

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

Instance Method Details

#and_return(*args) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mspec/mocks/proxy.rb', line 111

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



125
126
127
128
# File 'lib/mspec/mocks/proxy.rb', line 125

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

#any_number_of_timesObject



97
98
99
100
# File 'lib/mspec/mocks/proxy.rb', line 97

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

#argumentsObject



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

def arguments
  @arguments
end

#at_least(n) ⇒ Object



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

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

#at_most(n) ⇒ Object



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

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

#calledObject



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

def called
  @calls = calls + 1
end

#callsObject



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

def calls
  @calls ||= 0
end

#countObject



43
44
45
# File 'lib/mspec/mocks/proxy.rb', line 43

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

#exactly(n) ⇒ Object



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

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

#mock?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/mspec/mocks/proxy.rb', line 35

def mock?
  @type == :mock
end

#onceObject



89
90
91
# File 'lib/mspec/mocks/proxy.rb', line 89

def once
  exactly 1
end

#returningObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/mspec/mocks/proxy.rb', line 51

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)


39
40
41
# File 'lib/mspec/mocks/proxy.rb', line 39

def stub?
  @type == :stub
end

#timesObject



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

def times
  self
end

#twiceObject



93
94
95
# File 'lib/mspec/mocks/proxy.rb', line 93

def twice
  exactly 2
end

#with(*args) ⇒ Object

Raises:

  • (ArgumentError)


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

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

#yieldingObject



130
131
132
# File 'lib/mspec/mocks/proxy.rb', line 130

def yielding
  @yielding
end

#yielding?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/mspec/mocks/proxy.rb', line 134

def yielding?
  !@yielding.empty?
end