Module: RR::Extensions::InstanceMethods

Included in:
Adapters::Rspec, Adapters::TestUnit
Defined in:
lib/rr/extensions/instance_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 Scenario#times.

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


160
161
162
# File 'lib/rr/extensions/instance_methods.rb', line 160

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


168
169
170
# File 'lib/rr/extensions/instance_methods.rb', line 168

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


192
193
194
# File 'lib/rr/extensions/instance_methods.rb', line 192

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

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

Sets up a DoNotAllowCreator that generates a Double Scenario that expects never to be called.

do_not_allow(object).method_name


151
152
153
# File 'lib/rr/extensions/instance_methods.rb', line 151

def do_not_allow(subject, method_name=nil, &definition)
  RR::Space.instance.do_not_allow_creator(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


203
204
205
# File 'lib/rr/extensions/instance_methods.rb', line 203

def duck_type(*args)
  RR::WildcardMatchers::DuckType.new(*args)
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


176
177
178
# File 'lib/rr/extensions/instance_methods.rb', line 176

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

#mock(object, method_name = nil, &definition) ⇒ Object

When passed the object, this method returns a MockCreator that generates a Double Scenario that acts like a mock.

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

When passed the object and the method_name, this method returns a mock Scenario with the method already set.

mock also takes a block for definitions.

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


28
29
30
# File 'lib/rr/extensions/instance_methods.rb', line 28

def mock(object, method_name=nil, &definition)
  RR::Space.instance.mock_creator(object, method_name, &definition)
end

#mock_probe(object, method_name = nil, &definition) ⇒ Object Also known as: probe

When passed the object, this method returns a MockProbeCreator that generates a Double Scenario that acts like a mock probe.

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

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

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

mock_probe also takes a block for definitions.

mock_probe(object) 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 Scenario (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_probe(controller.template).render(:partial => "my/socks") do |html|
  html.should include("My socks are wet")
  "My new return value"
end


94
95
96
# File 'lib/rr/extensions/instance_methods.rb', line 94

def mock_probe(object, method_name=nil, &definition)
  RR::Space.instance.mock_probe_creator(object, 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


184
185
186
# File 'lib/rr/extensions/instance_methods.rb', line 184

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

#resetObject

Resets the registered Doubles and ordered Scenarios



11
12
13
# File 'lib/rr/extensions/instance_methods.rb', line 11

def reset
  RR::Space.instance.reset
end

#stub(object, method_name = nil, &definition) ⇒ Object

When passed the object, this method returns a StubCreator that generates a Double Scenario that acts like a stub.

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

When passed the object and the method_name, this method returns a stub Scenario with the method already set.

stub also takes a block for definitions.

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


45
46
47
# File 'lib/rr/extensions/instance_methods.rb', line 45

def stub(object, method_name=nil, &definition)
  RR::Space.instance.stub_creator(object, method_name, &definition)
end

#stub_probe(object, method_name = nil, &definition) ⇒ Object

When passed the object, this method returns a StubProbeCreator that generates a Double Scenario that acts like a stub probe.

stub_probe(User).new {|user| user}

stub_probe(User).new do |user|
  mock(user).valid? {false}
  user
end

stub_probe(User).new do |user|
  mock_probe(user).friends {|friends| friends[0..3]}
  user
end

Passing a block 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_probe(User) do
  new {|user| user}

  new do |user|
    mock(user).valid? {false}
  end

  new do |user|
    mock_probe(user).friends {|friends| friends[0..3]}
    user
  end
end

Passing a block to the Scenario (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.

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


141
142
143
# File 'lib/rr/extensions/instance_methods.rb', line 141

def stub_probe(object, method_name=nil, &definition)
  RR::Space.instance.stub_probe_creator(object, method_name, &definition)
end

#verifyObject

Verifies all the Double objects have met their TimesCalledExpectations.



6
7
8
# File 'lib/rr/extensions/instance_methods.rb', line 6

def verify
  RR::Space.instance.verify_doubles
end