freerange/mocha — pull request #195 by mrsimo (2014-04-02 14:36:18+00:00)
Title: Fix stubbing prepended methods (2nd try)

So I tried the branch from #194 with our own application, and there were a bunch of errors that the acceptance test suite didn't catch. Stuff would fail after stubbing `#find` of an active record object. I don't know how things happen in rails, but the ancestry chain of objects that inherit from `ActiveRecord::Base` is quite crazy.

Anyhow, I decided to see if rspec fixed this, and found out they solved it by prepending a module of their own, where they define the new (stubbed) method. Since the newly prepended module takes precedence over the other prepended modules, voilá!

I did a first try by always adding this prepended module, but the benchmarks in the test suite became crazy slow. So it's now using the prepended module only if there's a suspect prepended module, and things are speedy again.

This branch does pass all the tests on our rails app, in both 2.0 and 2.1 with no remarkable speed difference. Including the original problem that triggered me to try and fix these.

I'd like to favor this implementation over the one at #194, since that one doesn't work, and it seems a lot harder to understand than this one.

What do you say?