Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Access the current mocking adapter / Cucumber compatibility #1480

Closed
lime opened this issue Apr 10, 2014 · 6 comments
Closed

Access the current mocking adapter / Cucumber compatibility #1480

lime opened this issue Apr 10, 2014 · 6 comments

Comments

@lime
Copy link

lime commented Apr 10, 2014

In #1188 @myronmarston switched from using RSpec::Core::MockFrameworkAdapter to separate RSpec::Core::MockingAdapters for each mocking framework.

Is there / should there be a way to access the currently used adapter?

I ask particularly because I'm anticipating an incompatibility with Cucumber, since they currently access RSpec::Core::MockFrameworkAdapter directly in lib/cucumber/rspec.doubles.rb:4. As it stands now, Cucumber 1.3.14 will not work with RSpec 3.0.0.beta2.

I'm new to the RSpec codebase, so forgive me if I missed something.

@myronmarston
Copy link
Member

You can access the adapter for the currently configured mock framework using RSpec.configuration.mock_framework. RSpec::Core::MockFrameworkAdapter was always intended to be an implementation detail and not a public API anything external would rely upon.

Anyhow, why does cucumber even load rspec-core? rspec-core is the test runner and isn't intended to be loaded by other test runners. rspec-expectations and rspec-mocks are both pieces that an alternate test runner like cucumber can load and use on their own.

@mattwynne -- do you know why cucumber loads rspec-core?

@mattwynne
Copy link

That file https://github.com/cucumber/cucumber/blob/master/lib/cucumber/rspec/doubles.rb#L4 was added before my time and I wasn't actually aware it was there.

It would need to be explicitly required by a client project, as in this example

I believe the purpose of this file is to allow people to use RSpec mocks in their Cucumber step defs, and have the mock assertions get checked at the end of the scenario. That's not a practice I would use myself, but I guess it's a facility someone must have asked for.

Is there a way to do that 'properly' via RSpec's API?

Would it make more sense for this code to move into rspec-mocks, so people have to require rspec/mocks/cucumber instead of cucumber/rspec/doubles? That way you can make sure it changes in sync with your API.

@myronmarston
Copy link
Member

Is there a way to do that 'properly' via RSpec's API?

Yep. It's what the rest of the file is already doing. (At least, the rest of the file is using the RSpec 2.x API for it). Specifically, this is what was needed in RSpec 2:

require 'rspec/mocks'

# This line may actually not be needed, depending on what `self` is in the `Before` hook,
# as in rspec 2, `RSpec::Mocks.setup` would auto-include the module in the singleton class
# of the passed arg.
World(RSpec::Mocks::ExampleMethods)

Before do
  RSpec::Mocks::setup(self)
end

After do
  begin
    RSpec::Mocks::verify
  ensure
    RSpec::Mocks::teardown
  end
end

In RSpec 3, this has changed very slightly: you're responsible for including RSpec::Mocks::ExampleMethods yourself, and setup no longer accepts an arg:

require 'rspec/mocks'
World(RSpec::Mocks::ExampleMethods)

Before do
  RSpec::Mocks.setup
end

After do
  begin
    RSpec::Mocks.verify
  ensure
    RSpec::Mocks.teardown
  end
end

Would it make more sense for this code to move into rspec-mocks, so people have to require rspec/mocks/cucumber instead of cucumber/rspec/doubles? That way you can make sure it changes in sync with your API.

Maybe. Currently rspec-mocks doesn't have any logic in it to auto-integrate with any frameworks (not even with rspec-core -- it's rspec-core that takes care of that). The API I've shown above for RSpec 3 will be in place for a long time to come -- at least until RSpec 4 (as per semver) and we have no plans to ever change it further so potentially for much longer after that.

@myronmarston
Copy link
Member

I don't think there's anything for us to change in rspec for this so I'm going to close it. Let me know if there are further questions.

@mattwynne
Copy link

@lime would you please create a ticket on Cucumber's issue tracker for this?

@lime
Copy link
Author

lime commented Apr 16, 2014

Sure! I'm not entirely sure how to phrase it, but I'll give it a shot. ;)

wstephenson pushed a commit to wstephenson/cucumber that referenced this issue Jul 16, 2014
RSpec::Core::MockFrameworkAdapter is not intended to be used outside
rspec-core itself, RSpec::Mocks::ExampleMethods should fulfil the
same purpose.

See rspec/rspec-core#1480
Themitchell pushed a commit to alphagov/finder-frontend that referenced this issue Oct 16, 2014
The documented solution for including rspec here
https://github.com/cucumber/cucumber/wiki/RSpec-Expectations
does not seem to apply.

This solution however seems to work:
rspec/rspec-core#1480
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants