Undefining a method in Ruby is pretty simple, I can just use undef METHOD_NAME
.
Is there anything similar for a class? I am on MRI 1.9.2
.
I have to undefine an ActiveRecord Model, run two lines of code, and restore the model back to its original form.
The problem is, I have an model Contact
and I am using a company's API and it happens that they have some class called Contact
, and changing my model name would be lot of work for me.
What can I do in this situation?
In a similar situation - mocking a class used internally by another class I'm trying to test - I found this to be a workable solution:
I would have thought there would be a better way to do this - i.e. I couldn't see a way to pass in the desired return value for reuse - but this seems good enough for now. I'm new to mocking/factories, and I'd love to hear about any alternative methods.
Edit:
Ok, so not so similar after all.
I found a better way using RSpec mock, thanks to an excellent explanation in the RSpec Google Group:
EDIT Just noticed your edit, removing the constant is probably not the best way to achieve what you're looking for. Why not just move one of the
Contact
classes into a separate namespace.EDIT2 You could also rename your class temporarily like this:
Again, the trouble with this is that you'll still have the newer
Contact
class so you'll have to remove that again. I would really recommend name-spacing your classes instead. This will also help you avoid any loading issues