Created
April 25, 2011 17:36
-
-
Save dmcinnes/940868 to your computer and use it in GitHub Desktop.
Rspec Stubbing corner case
This file contains hidden or 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
~/tmp $ ruby -v | |
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0] | |
~/tmp $ spec -v | |
rspec 1.3.2 | |
~/tmp $ spec test.rb | |
..F | |
1) | |
NoMethodError in 'TestClass fails after the stub' | |
undefined method `foo' for #<TestClass:0x00000101180158> | |
test.rb:28:in `block (2 levels) in <top (required)>' | |
Finished in 0.046678 seconds | |
3 examples, 1 failure | |
~/tmp $ rspec -v | |
2.5.1 | |
~/tmp $ rspec test.rb | |
... | |
Finished in 0.00154 seconds | |
3 examples, 0 failures | |
~/tmp $ |
This file contains hidden or 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
class TestClass | |
end | |
# our use case is with a stub on a constant | |
TEST = TestClass.new | |
describe TestClass do | |
it "works before the stub" do | |
TestClass.send(:attr_accessor, :foo) | |
TEST.foo = :baz | |
TEST.foo.should == :baz | |
TestClass.send(:remove_method, 'foo') | |
TestClass.send(:remove_method, 'foo=') | |
end | |
it "has a stubbed method" do | |
TEST.stub! :foo => :bar | |
TEST.foo.should == :bar | |
end | |
it "fails after the stub" do | |
TestClass.send(:attr_accessor, :foo) | |
TEST.foo = :baz | |
TEST.foo.should == :baz | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment