Skip to content
This repository

Support multiple mocking frameworks #683

Open
dchelimsky opened this Issue September 18, 2012 · 5 comments

5 participants

David Chelimsky Myron Marston David Leal Jim Weirich Cory Foy
David Chelimsky
Owner

Switching mocking frameworks in a suite is currently an "all at once" deal because there is no support for using more than one mocking framework at a time.

@jimweirich is working on a workaround for this in flexmock, but it depends on paths that are internal to rspec: https://gist.github.com/3738752

Let's consider making the adapters shipped w/ RSpec composable, so that we can support more than one, e.g.

RSpec.configure do |c|
  c.mock_with :rspec, :flexmock, :mocha, :rr
end
David Chelimsky
Owner

I realize, btw, that there are serious downsides to consider. The biggest problem I see is that rspec-mocks now supports any_instance, which is also supported by mocha. If we compose the adapters as in @jimweirich's gist (linked above) then the last adapter to attach any_instance to Object will win, which would constrain such a transition.

Also, it is very likely that people will try to use this feature to mix/match in different parts of the same suite (vs a transition) and that's a recipe for confusion.

So I'm not 100% in on this, but I wanted to get the conversation going here rather than twitter.

David Leal

Assuming that this is only useful if you plan to migrate to a different mock framework, and assuming you will migrate whole files at once (and I don't see why you wouldn't), why not move the mock configuration to different files, and require the appropriate file? E.g.

# file using old mock framework
require 'spec_helper'
require 'old_mock'

# file using new mock framework
require 'spec_helper'
require 'new_mock'

# old_mock.rb
RSpec.configure { |c| c.mock_with :old_mock }

# new_mock.rb
RSpec.configure { |c| c.mock_with :new_mock }

I'm also assuming that rspec can't switch mock frameworks in real time (i.e., using a metadata switch to control it). Otherwise, this would be even easier. In any case, no new code needed.

Am I missing something?

David Leal

Ah, I suppose the require solution would only work if you were running specs on a single file at a time. Scratch that, then. Sorry about the noise. :)

Jim Weirich

I wouldn't assume that any given mock framework is composable with another arbitrary framework, and I don't think there is much we can do about that without changes to the individual frameworks.

FlexMock, for its part, tries to stay out of the global namespace as much as possible. It is possible to use FlexMock mocks and another framework's mocks in the same test. You should only run into problems if you try to mock out methods on the same real object using two different frameworks.

Gimme is a spy framework that is pretty unobtrusive, it too should be fairly composable.

So while not all frameworks are composable, there are certainly combinations that could work together.

Cory Foy

This might be a bit much, but if someone is using this as a transition, could we include a configuration option of the preferred framework? So it would try that one first, and otherwise send it down the chain (or, in this case, up the chain since the last one defined wins)?

Myron Marston myronmarston referenced this issue from a commit November 15, 2013
Myron Marston Rearrange mocking adapters.
* Use a different module name for each adapter.
* Name the files based on the module name.

This is a stepping stone towards supporting the user
of multiple mocking adapters -- see #683.
For more background, see these gists:

* https://gist.github.com/myronmarston/5584693
* https://gist.github.com/jimweirich/3738752
45eac96
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.