Skip to content

Instantly share code, notes, and snippets.

@dmcinnes
Created April 25, 2011 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmcinnes/940868 to your computer and use it in GitHub Desktop.
Save dmcinnes/940868 to your computer and use it in GitHub Desktop.
Rspec Stubbing corner case
~/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 $
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