Skip to content

Instantly share code, notes, and snippets.

@mark
Created August 1, 2014 18:51
Show Gist options
  • Save mark/99d098f66c7135ddc8d2 to your computer and use it in GitHub Desktop.
Save mark/99d098f66c7135ddc8d2 to your computer and use it in GitHub Desktop.
Redefining a stubbed method, using Mocha
require 'minitest/spec'
require 'mocha/setup'
require 'minitest/autorun'
class MyClass
class << self
attr_accessor :bar
end
def self.foo
self.bar = :baz
end
end
describe "MyClass" do
subject do
MyClass.tap { MyClass.bar = nil }
end
it "will stub out foo" do
subject.stubs(:foo).returns(:quux)
subject.foo.must_equal :quux
end
it "doesn't set bar" do
subject.stubs(:foo).returns(:quux)
subject.foo
subject.bar.must_be_nil
end
it "can be replaced" do
subject.stubs(:foo)
def subject.foo
self.bar = :boosh
end
subject.foo
subject.bar.must_equal :boosh
end
it "replacement isn't permanent" do
subject.stubs(:foo)
def subject.foo
self.bar = :boosh
end
subject.unstub(:foo)
subject.foo
subject.bar.must_equal :baz
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment