-
-
Save MarkMenard/740783 to your computer and use it in GitHub Desktop.
# My first attempt at using both Mocha and RSpec mocks in a project. | |
module Spec | |
module Adapters | |
module MockFramework | |
# Mocha::Standalone was deprecated as of Mocha 0.9.7. | |
begin | |
include Mocha::API | |
rescue NameError | |
include Mocha::Standalone | |
end | |
include Spec::Mocks::ExampleMethods | |
def setup_mocks_for_rspec | |
mocha_setup | |
$rspec_mocks ||= Spec::Mocks::Space.new | |
end | |
def verify_mocks_for_rspec | |
mocha_verify | |
$rspec_mocks.verify_all | |
end | |
def teardown_mocks_for_rspec | |
mocha_teardown | |
$rspec_mocks.reset_all | |
end | |
end | |
end | |
end |
Yea, this experiment didn't pan out. At first I thought it was working, but eventually I realized it was crossing the streams. I might take another crack at it by doing some method aliasing and working on it in a test project rather than this larger project I'm working on. I think it'd be really nice if a spec could pick its mocking framework, but that seems like something the new scoped monkey patching thing would be needed for.
Currently I've fallen back to using RSpec mocks (1.3) on my Rails 2.3 project, but and this is ubber strange, if we have the Mocha gem installed it gets loaded by rspec-rails at some point and one spec I wrote in Mocha to see if things would work ok, is working fine using Mocha mocking: expect, returns, etc. It's totally weird.
That's because Rails will load mocha if it's present on the $LOAD_PATH, regardless of whether you configure it or not. If you configure your rails-2 app w/ bundler, this problem goes away.
Bundler.... Oh the rabbit hole that turned into on this project. We're running in a Java web application embedded inside a Struts 1 app. (This is the beginning of the migration away from Struts.) We tried Bundler and had no end of issues. I've forked Bundler to start messing around with it and see if I can figure it out. I'm also not to enthused with the Bundler war deployment story. (Therefore forking Bundler to spend some time on it.)
The other thing I thought about was aliasing the mocha #mock, #stub, etc. methods to mocha_mock# or some such convention.
What happens when you say "foo = mock('foo')" in an example? I'm guessing it's either always a mocha mock object (so when you say should_receive it barks) or always an rspec mock object (in which case it would bark when you say expects). You seeing something better than that?