Is there a way to undo Mocha stubbing of any_instance in Test::Unit

Craig Walker picture Craig Walker · May 24, 2010 · Viewed 10.2k times · Source

Much like this question, I too am using Ryan Bates's nifty_scaffold. It has the desirable aspect of using Mocha's any_instance method to force an "invalid" state in model objects buried behind the controller.

Unlike the question I linked to, I'm not using RSpec, but Test::Unit. That means that the two RSpec-centric solutions there won't work for me.

Is there a general (ie: works with Test::Unit) way to remove the any_instance stubbing? I believe that it's causing a bug in my tests, and I'd like to verify that.

Answer

Craig Walker picture Craig Walker · Dec 21, 2011

As it happens, Mocha 0.10.0 allows unstubbing on any_instance().

str = "Not Stubbed!"
String.any_instance.stubs(:to_s).returns("Stubbed!")
puts str.to_s   # "Stubbed!"
String.any_instance.unstub(:to_s)
puts str.to_s   # "Not Stubbed!"