Replacing a class with a test double with Rspec
Say you want to write a test that verifies no class methods are called (i.e., a class isn’t being used). With Rspec’s mocking library, you can easily replace methods with message expectations that verify a method is called, but making sure no methods are called takes a little doing.
What you need is an object that will raise an error if there are any unexpected messages. Rspec’s test double will do this by default. Combining this with stub_const to replace the class will allow you to replace the class with a test double:
stub_const("ClassName", double("my test double"))
Now, it will only respond to messages you’ve defined on the test double (in this example, none), so any use of the class you haven’t anticipated will cause the test to fail.
For more examples of replacing a constants with stub_const, read Myron Marston’s post Constant Stubbing in RSpec 2.11.