Skip Mocha
Yesterday I confessed how messed up was CachedModels 0.0.2. The problem was pretty simple: I made some mistakes mocking up Memcached behaviors.
The Dilemma
I massively use Mocha for track how many calls CachedModels performs against the cache, and (ideally) for run test suite without any server active. But, this approach led me to broken results. Should I keep out Mocha from my tests?
The Solution
Why don’t mock the mock? We know, Ruby allows to add behaviors to classes at runtime, Mocha itself take this advantage.
I added to my end class NilClass
def method_missing(method_name, *args, &block)
end
end
end
test_helper.rb the following code:
if ENV['SKIP_MOCHA'] == 'true'
class Object
def expects(*args)
self
end
def method_missing(method_name, *args, &block)
end
Now I can safely run tests without Mocha:
$ rake cached_models SKIP_MOCHA=true