Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created May 15, 2013 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myronmarston/5584693 to your computer and use it in GitHub Desktop.
Save myronmarston/5584693 to your computer and use it in GitHub Desktop.
Mock with multiple frameworks based on metadata. In reply to https://twitter.com/_solnic_/status/334682956771778560
# This is totally untested, but something like this should work...
module MultipleMockFrameworksAdapters
extend Forwardable
def_delegators :mock_framework_for_current_example,
# these are the 3 methods your adapter needs
:setup_mocks_for_rspec,
:verify_mocks_for_rspec,
:teardown_mocks_for_rspec
def mock_framework_for_current_example
case $current_rspec_example.metadata[:mock_with]
when :mocha
# Unfortunately, in https://github.com/rspec/rspec-core/tree/master/lib/rspec/core/mocking
# Each adapter is defined as the same module, so you can't just delegate to the existing
# ones here. That should be fixed, though; I'd happily accept a PR addressing that!
# So, you'll need to define these based on the ones in the directory linked to above.
MochaAdapter
else
RSpecMocksAdapter
end
end
end
RSpec.configure do |rspec|
# Tell rspec to use your adapter
rspec.mock_with MultipleMockFrameworksAdapters
# Finally, make the current example globally available so you can branch on its
# metadata in your adapter above.
rspec.before(:each) do
$current_rspec_example = example
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment