Skip to content

Mocking and Stubbing with Cucumber

bjoernalbers edited this page Nov 2, 2011 · 18 revisions

Mocking is usually discouraged when using Cucumber. You can set up mocks with expectations in your Step Definitions, but ideally you would exercise as much of your stack as possible. If you have a dependency on an external system we recommend using stubs instead of mocks.

RSpec 2.x

Starting with Cucumber 0.8.4 you can use all of RSpec’s supported mocking frameworks (RSpec, Mocha, RR, Flexmock). Just require 'cucumber/rspec/doubles' (Test-Double is a more generic term than mocks and stubs).

Check out Cucumber’s example to see how it’s used.

RSpec 1.2.8-1.3.0

As of RSpec 1.2.8 if you need to use stubs within Cucumber and you are using the built in mocking/stubbing framework that ships with RSpec you simply need to require 'spec/stubs/cucumber' in your env.rb file. Note that message expectations (should_receive) are not automatically verified.

Existing stubbing/mocking frameworks only support in-process stubbing & mocking, cross-stub provides simple cross process stubbing, which is useful when running cucumber with selenium, culerity, steam, etc.
Aruba-Doubles lets you stub command line applications with Cucumber.

We recommend you only stub things like Time and external services like OpenID etc.

Clone this wiki locally