Module: BooticClient::Stubbing::Stubber

Included in:
Stub, StubRoot
Defined in:
lib/bootic_client/stubbing.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/bootic_client/stubbing.rb', line 32

def method_missing(method_name, *args, &block)
  opts = stringify_keys(args.first)
  if stub = stubs[stub_key(method_name, opts)]
    stub.returns? ? stub.returns : stub
  else
    raise MissingStubError, "No method stubbed for '#{method_name}' with options #{opts.inspect}"
  end
end

Instance Method Details

#from_hash(h) ⇒ Object



6
7
8
# File 'lib/bootic_client/stubbing.rb', line 6

def from_hash(h)
  self
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/bootic_client/stubbing.rb', line 41

def respond_to_missing?(method_name, include_private = false)
  stubs.keys.any?{|k|
    k.to_s =~ /^#{method_name.to_s}/
  }
end

#stub(method_name, opts = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/bootic_client/stubbing.rb', line 22

def stub(method_name, opts = {})
  key = stub_key(method_name, opts)
  if st = stubs[key]
    st.stub(method_name, opts)
    st
  else
    stubs[key] = Stub.new(method_name, opts)
  end
end

#stub_chain(method_path, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bootic_client/stubbing.rb', line 10

def stub_chain(method_path, opts = {})
  meths = method_path.split('.')
  c = 0
  opts = stringify_keys(opts)

  meths.reduce(self) do |stub, method_name|
    c += 1
    a = c == meths.size ? opts : {}
    stub.stub(method_name, a)
  end
end