7

I am using Cucumber as the BDD framework with rspec/mocha mocking. Ideally we would not mock/stub behavior in cucumber specs; however the scenario is exceptional here. To give you the brief idea of problem here; I have two features product feature and cart feature.

Cart feature is currently mocking some of the product fetch from 3 party system. And in cucumber run cart feature runs before product feature. Product feature uses real call for some scenarios. Ideally we would expect that Class level stubs like Product.stub(:find) would be cleared once the scenarios are completed; however that is not the case. Class level stubs linger on to the next cucumber scenario run and interfere with the call.

I interchangeably used mocha and rspec to induce the stubs; but no avail; in either case the stub remains and prevents my product feature to run after cart feature. Both of them run fine in isolation.

Has anyone faced similar issue of stubs not being reset in cucumber? Is there a way, I can restore the original class behavior?

Any help will be appreciated.

Cheers

2 Answers 2

8

For mocha integration, you should add the following to a file in your features/support folder (like mocha.rb)

require "mocha"

World(Mocha::Standalone)

Before do
  mocha_setup
end

After do
  begin
    mocha_verify
  ensure
    mocha_teardown
  end
end

The after block should release all mocks.

Source: http://gist.github.com/80554

1
  • awesome. Just what I needed to dump rspec mocking/stubbing and go with mocha! :)
    – Priyank
    Jul 10, 2010 at 15:05
4

Syntax is slightly different for mocha 1.1:

require 'mocha/api'

World(Mocha::API)

Before do
  mocha_setup
end

After do
  begin
    mocha_verify
  ensure
    mocha_teardown
  end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.