Created
May 15, 2013 15:09
-
-
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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