RSpec recipe: remove constants created during a test execution

Sometime you want to create a fake object for a test, it can be especialy the case when you are creating a ruby gem. You need to declare a some classes, structs maybe even modules and use them inside your tests. But it is important to maintain a clean state between tests. Declared classes, modules, structs are global constants, they pollute global namespace. This is a problem.

In RSpec you can solve this using an around hook and remove all constants declared during a test. Here is the solution:

I very wellcome any suggestions and criticism. You can play around with the code by cloning the repo.

November 26 Update:

Robert @lobatifricha point a better way if you only need to pass created class as an argument to a method that you are testing.