Module: Spec::Mocks::Methods
Instance Method Summary collapse
- #as_null_object ⇒ Object
- #null_object? ⇒ Boolean
-
#received_message?(sym, *args, &block) ⇒ Boolean
:nodoc:.
-
#rspec_reset ⇒ Object
:nodoc:.
-
#rspec_verify ⇒ Object
:nodoc:.
- #should_not_receive(sym, &block) ⇒ Object
- #should_receive(sym, opts = {}, &block) ⇒ Object
- #stub!(sym_or_hash, opts = {}, &block) ⇒ Object (also: #stub)
-
#stub_chain(*methods) ⇒ Object
:call-seq: object.stub_chain(:first, :second, :third).and_return(:this).
- #unstub!(message) ⇒ Object (also: #unstub)
Instance Method Details
#as_null_object ⇒ Object
70 71 72 |
# File 'lib/spec/mocks/methods.rb', line 70 def as_null_object __mock_proxy.as_null_object end |
#null_object? ⇒ Boolean
74 75 76 |
# File 'lib/spec/mocks/methods.rb', line 74 def null_object? __mock_proxy.null_object? end |
#received_message?(sym, *args, &block) ⇒ Boolean
:nodoc:
58 59 60 |
# File 'lib/spec/mocks/methods.rb', line 58 def (sym, *args, &block) #:nodoc: __mock_proxy.(sym.to_sym, *args, &block) end |
#rspec_reset ⇒ Object
:nodoc:
66 67 68 |
# File 'lib/spec/mocks/methods.rb', line 66 def rspec_reset #:nodoc: __mock_proxy.reset end |
#rspec_verify ⇒ Object
:nodoc:
62 63 64 |
# File 'lib/spec/mocks/methods.rb', line 62 def rspec_verify #:nodoc: __mock_proxy.verify end |
#should_not_receive(sym, &block) ⇒ Object
8 9 10 |
# File 'lib/spec/mocks/methods.rb', line 8 def should_not_receive(sym, &block) __mock_proxy.(caller(1)[0], sym.to_sym, &block) end |
#should_receive(sym, opts = {}, &block) ⇒ Object
4 5 6 |
# File 'lib/spec/mocks/methods.rb', line 4 def should_receive(sym, opts={}, &block) __mock_proxy.(opts[:expected_from] || caller(1)[0], sym.to_sym, opts, &block) end |
#stub!(sym_or_hash, opts = {}, &block) ⇒ Object Also known as: stub
12 13 14 15 16 17 18 |
# File 'lib/spec/mocks/methods.rb', line 12 def stub!(sym_or_hash, opts={}, &block) if Hash === sym_or_hash sym_or_hash.each {|method, value| stub!(method).and_return value } else __mock_proxy.add_stub(caller(1)[0], sym_or_hash.to_sym, opts, &block) end end |
#stub_chain(*methods) ⇒ Object
:call-seq:
object.stub_chain(:first, :second, :third).and_return(:this)
Supports stubbing a chain of methods. Each argument represents a method name to stub, and each one returns a proxy object that can accept more stubs, until the last, which returns whatever is passed to +and_return_.
Examples
# with this in an example ...
article = double('article')
Article.stub_chain(:authored_by, :published, :recent).and_return([article])
# then this will return an Array with the article double in it:
Article.(params[:author_id]).published.recent
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/spec/mocks/methods.rb', line 43 def stub_chain(*methods) if methods.length > 1 if matching_stub = __mock_proxy.find_matching_method_stub(methods[0]) methods.shift matching_stub.invoke_return_block.stub_chain(*methods) else next_in_chain = Object.new stub!(methods.shift) {next_in_chain} next_in_chain.stub_chain(*methods) end else stub!(methods.shift) end end |
#unstub!(message) ⇒ Object Also known as: unstub
22 23 24 |
# File 'lib/spec/mocks/methods.rb', line 22 def unstub!() __mock_proxy.remove_stub() end |