Skip to content

Instantly share code, notes, and snippets.

@floehopper
Created July 25, 2011 14:24
Show Gist options
  • Save floehopper/1104229 to your computer and use it in GitHub Desktop.
Save floehopper/1104229 to your computer and use it in GitHub Desktop.
Alternative to a selective Mocha::ClassMethods#any_instance
require "test/unit"
require "rubygems"
require "mocha"
class Foo
attr_reader :argument
def initialize(argument)
@argument = argument
end
def bar
"bar"
end
end
class FooTest < Test::Unit::TestCase
def test_foo
foo_one = Foo.new(1)
foo_one.stubs(:bar).returns("one")
foo_two = Foo.new(2)
foo_two.stubs(:bar).returns("two")
default_foo = Foo.new(nil)
Foo.stubs(:new).returns(default_foo)
Foo.stubs(:new).with(1).returns(foo_one)
Foo.stubs(:new).with(2).returns(foo_two)
assert_equal 1, Foo.new(1).argument
assert_equal "one", Foo.new(1).bar
assert_equal 2, Foo.new(2).argument
assert_equal "two", Foo.new(2).bar
assert_equal nil, Foo.new(3).argument
assert_equal "bar", Foo.new(3).bar
end
end
# $ ruby foo_test.rb
# Loaded suite foo_test
# Started
# .
# Finished in 0.001324 seconds.
#
# 1 tests, 6 assertions, 0 failures, 0 errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment