Module: RR::Adapters::RRMethods

Included in:
Rspec, TestUnit
Defined in:
lib/rr/adapters/rr_methods.rb

Instance Method Summary collapse

Instance Method Details

#any_timesObject

Returns a AnyTimesMatcher. This is meant to be passed in as an argument to Double#times.

mock(object).method_name(anything).times(any_times) {return_value}


176
177
178
# File 'lib/rr/adapters/rr_methods.rb', line 176

def any_times
  TimesCalledMatchers::AnyTimesMatcher.new
end

#anythingObject

Sets up an Anything wildcard ArgumentEqualityExpectation that succeeds when passed any argument.

mock(object).method_name(anything) {return_value}
object.method_name("an arbitrary value") # passes


184
185
186
# File 'lib/rr/adapters/rr_methods.rb', line 184

def anything
  RR::WildcardMatchers::Anything.new
end

#booleanObject

Sets up an Boolean wildcard ArgumentEqualityExpectation that succeeds when passed an argument that is a ::Boolean.

mock(object).method_name(boolean) {return_value}
object.method_name(false) # passes


208
209
210
# File 'lib/rr/adapters/rr_methods.rb', line 208

def boolean
  RR::WildcardMatchers::Boolean.new
end

#dont_allow(subject = DoubleCreator::NO_SUBJECT_ARG, method_name = nil, &definition) ⇒ Object Also known as: do_not_allow, dont_call, do_not_call

This method sets the Double to have a dont_allow strategy. A dont_allow strategy sets the default state of the Double to expect never to be called. The Double’s expectations can be changed.

The following example sets the expectation that subject.method_name will never be called with arg1 and arg2.

do_not_allow(subject).method_name(arg1, arg2)

dont_allow also supports a block sytnax.

dont_allow(subject) do |m|
  m.method1 # Do not allow method1 with any arguments
  m.method2(arg1, arg2) # Do not allow method2 with arguments arg1 and arg2
  m.method3.with_no_args # Do not allow method3 with no arguments
end


148
149
150
151
# File 'lib/rr/adapters/rr_methods.rb', line 148

def dont_allow(subject=DoubleCreator::NO_SUBJECT_ARG, method_name=nil, &definition)
  creator = RR::Space.double_creator
  creator.dont_allow(subject, method_name, &definition)
end

#duck_type(*args) ⇒ Object

Sets up a DuckType wildcard ArgumentEqualityExpectation that succeeds when passed the argument implements the methods.

arg = Object.new
def arg.foo; end
def arg.bar; end
mock(object).method_name(duck_type(:foo, :bar)) {return_value}
object.method_name(arg) # passes


219
220
221
# File 'lib/rr/adapters/rr_methods.rb', line 219

def duck_type(*args)
  RR::WildcardMatchers::DuckType.new(*args)
end

#instance_of(subject = DoubleCreator::NO_SUBJECT_ARG, method_name = nil, &definition) ⇒ Object

Calling instance_of will cause all instances of the passed in Class to have the Double defined.

The following example mocks all User’s valid? method and return false.

mock.instance_of(User).valid? {false}

The following example mocks and proxies User#projects and returns the first 3 projects.

mock.instance_of(User).projects do |projects|
  projects[0..2]
end


167
168
169
170
# File 'lib/rr/adapters/rr_methods.rb', line 167

def instance_of(subject=DoubleCreator::NO_SUBJECT_ARG, method_name=nil, &definition)
  creator = RR::Space.double_creator
  creator.instance_of(subject, method_name, &definition)
end

#is_a(klass) ⇒ Object

Sets up an IsA wildcard ArgumentEqualityExpectation that succeeds when passed an argument of a certain type.

mock(object).method_name(is_a(String)) {return_value}
object.method_name("A String") # passes


192
193
194
# File 'lib/rr/adapters/rr_methods.rb', line 192

def is_a(klass)
  RR::WildcardMatchers::IsA.new(klass)
end

#mock(subject = DoubleCreator::NO_SUBJECT_ARG, method_name = nil, &definition) ⇒ Object

This method sets the Double to have a mock strategy. A mock strategy sets the default state of the Double to expect the method call with arguments exactly one time. The Double’s expectations can be changed.

This method can be chained with proxy.

mock.proxy(subject).method_name_1
or
proxy.mock(subject).method_name_1

When passed the subject, a DoubleMethodProxy is returned. Passing a method with arguments to the proxy will set up expectations that the a call to the subject’s method with the arguments will happen, and return the prescribed value.

mock(subject).method_name_1 {return_value_1}
mock(subject).method_name_2(arg1, arg2) {return_value_2}

When passed the subject and the method_name, this method returns a mock Double with the method already set.

mock(subject, :method_name_1) {return_value_1}
mock(subject, :method_name_2).with(arg1, arg2) {return_value_2}

mock also takes a block for definitions.

mock(subject) do
  method_name_1 {return_value_1}
  method_name_2(arg_1, arg_2) {return_value_2}
end


43
44
45
46
# File 'lib/rr/adapters/rr_methods.rb', line 43

def mock(subject=DoubleCreator::NO_SUBJECT_ARG, method_name=nil, &definition)
  creator = RR::Space.double_creator
  creator.mock(subject, method_name, &definition)
end

#numericObject

Sets up an Numeric wildcard ArgumentEqualityExpectation that succeeds when passed an argument that is ::Numeric.

mock(object).method_name(numeric) {return_value}
object.method_name(99) # passes


200
201
202
# File 'lib/rr/adapters/rr_methods.rb', line 200

def numeric
  RR::WildcardMatchers::Numeric.new
end

#proxy(subject = DoubleCreator::NO_SUBJECT_ARG, method_name = nil, &definition) ⇒ Object

This method add proxy capabilities to the Double. proxy can be called with mock or stub.

mock.proxy(controller.template).render(:partial => "my/socks")

stub.proxy(controller.template).render(:partial => "my/socks") do |html|
  html.should include("My socks are wet")
  html
end

mock.proxy(controller.template).render(:partial => "my/socks") do |html|
  html.should include("My socks are wet")
  "My new return value"
end

mock.proxy also takes a block for definitions.

mock.proxy(subject) do
  render(:partial => "my/socks")

  render(:partial => "my/socks") do |html|
    html.should include("My socks are wet")
    html
  end

  render(:partial => "my/socks") do |html|
    html.should include("My socks are wet")
    html
  end

  render(:partial => "my/socks") do |html|
    html.should include("My socks are wet")
    "My new return value"
  end
end

Passing a block to the Double (after the method name and arguments) allows you to intercept the return value. The return value can be modified, validated, and/or overridden by passing in a block. The return value of the block will replace the actual return value.

mock.proxy(controller.template).render(:partial => "my/socks") do |html|
  html.should include("My socks are wet")
  "My new return value"
end


127
128
129
130
# File 'lib/rr/adapters/rr_methods.rb', line 127

def proxy(subject=DoubleCreator::NO_SUBJECT_ARG, method_name=nil, &definition)
  creator = RR::Space.double_creator
  creator.proxy(subject, method_name, &definition)
end

#resetObject

Resets the registered Doubles and ordered Doubles



11
12
13
# File 'lib/rr/adapters/rr_methods.rb', line 11

def reset
  RR::Space.instance.reset
end

#stub(subject = DoubleCreator::NO_SUBJECT_ARG, method_name = nil, &definition) ⇒ Object

This method sets the Double to have a stub strategy. A stub strategy sets the default state of the Double to expect the method call with any arguments any number of times. The Double’s expectations can be changed.

This method can be chained with proxy.

stub.proxy(subject).method_name_1
or
proxy.stub(subject).method_name_1

When passed the subject, a DoubleMethodProxy is returned. Passing a method with arguments to the proxy will set up expectations that the a call to the subject’s method with the arguments will happen, and return the prescribed value.

stub(subject).method_name_1 {return_value_1}
stub(subject).method_name_2(arg_1, arg_2) {return_value_2}

When passed the subject and the method_name, this method returns a stub Double with the method already set.

mock(subject, :method_name_1) {return_value_1}
mock(subject, :method_name_2).with(arg1, arg2) {return_value_2}

stub also takes a block for definitions.

stub(subject) do
  method_name_1 {return_value_1}
  method_name_2(arg_1, arg_2) {return_value_2}
end


77
78
79
80
# File 'lib/rr/adapters/rr_methods.rb', line 77

def stub(subject=DoubleCreator::NO_SUBJECT_ARG, method_name=nil, &definition)
  creator = RR::Space.double_creator
  creator.stub(subject, method_name, &definition)
end

#verifyObject

Verifies all the DoubleInjection objects have met their TimesCalledExpectations.



6
7
8
# File 'lib/rr/adapters/rr_methods.rb', line 6

def verify
  RR::Space.instance.verify_doubles
end