Skip to content

Instantly share code, notes, and snippets.

@benpickles
Created January 25, 2011 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benpickles/794959 to your computer and use it in GitHub Desktop.
Save benpickles/794959 to your computer and use it in GitHub Desktop.
require 'test/unit'
require 'rubygems'
require 'mocha'
class Foo
def self.say_hello_to(name)
Bar.hello(name)
end
end
class Bar
end
class TestFooWithStubbingInSetup < Test::Unit::TestCase
def setup
@hello = mock("said hello")
Bar.stubs(:hello).returns(@hello)
end
def test_something
Bar.expects(:hello).with("zab")
Foo.say_hello_to("baz")
end
def test_something_else
assert_equal @hello, Foo.say_hello_to("bob")
end
# Lots of other tests so it's convenient to stub Bar.hello in the setup...
end
class TestFooWithoutStubbingInSetup < Test::Unit::TestCase
def setup
@hello = mock("said hello")
end
def test_something
Bar.expects(:hello).with("zab")
Foo.say_hello_to("baz")
end
def test_something_else
Bar.stubs(:hello).returns(@hello)
assert_equal @hello, Foo.say_hello_to("bob")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment