how to stub methods in cucumber

Most of the Cucumber tests that I’ve written don’t need stubbing. But the other day I was trying to test a particular feature which integrated with an external service.

Then, stubbing a call to the external service seemed like the best way to go. Fortunately, since Cucumber 0.8.4, mocking and stubbing is now possible without hacks. See: Mocking and Stubbing with Cucumber.

Here is the gist.

Add this line to your env.rb

1
require 'spec/stubs/cucumber'

Start stubbing methods in your steps.rb files

For example:

1
2
ExternalService.should_receive(:send).and_return(my_response)
User.should_receive(:customer_add_success?).and_return(true)

Next thing I’d like to do is use stubbing to test features in the future. For example: Test a feature involving recurring events around daylight time savings time.

This entry was posted in open source, programming, Ruby programming, software engineering and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free