Class: Quickie::Mock

Inherits:
Object show all
Defined in:
lib/quickie_motion/mock.rb

Instance Method Summary collapse

Constructor Details

#initializeMock

Returns a new instance of Mock.



22
23
24
# File 'lib/quickie_motion/mock.rb', line 22

def initialize
  @stash = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/quickie_motion/mock.rb', line 49

def method_missing(method, *args)
  if @stash.key?(method) then
    @stash[method][:return]
  else
    raise NoMethodError, "undefined method `%s', expected one of %p" % [ method, methods ]
  end
end

Instance Method Details

#__methodsObject



8
# File 'lib/quickie_motion/mock.rb', line 8

alias :__methods     :methods

#__respond_to?Object



9
# File 'lib/quickie_motion/mock.rb', line 9

alias :__respond_to? :respond_to?

#inspectObject



37
38
39
# File 'lib/quickie_motion/mock.rb', line 37

def inspect
  self.to_s
end

#instance_variablesObject



41
42
43
# File 'lib/quickie_motion/mock.rb', line 41

def instance_variables
  []
end

#methodsObject



45
46
47
# File 'lib/quickie_motion/mock.rb', line 45

def methods
  (__methods - [ :__methods, :__respond_to?, :instance_variables, :respond_to_missing? ] + @stash.keys).sort
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/quickie_motion/mock.rb', line 57

def respond_to?(method)
  return true if @stash.key?(method.to_sym)
  __respond_to?(method)
end

#stub(method, options = {}) ⇒ Object Also known as: stub!



26
27
28
29
30
31
32
33
34
# File 'lib/quickie_motion/mock.rb', line 26

def stub(method, options = {})
  unless __methods.include?(method)
    options[:return] ||= nil
    @stash[method] = options
  else
    super(method, options)
  end
  self
end