Created
August 1, 2014 18:51
-
-
Save mark/99d098f66c7135ddc8d2 to your computer and use it in GitHub Desktop.
Redefining a stubbed method, using Mocha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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