Thursday, February 10, 2011

Rails 3.0: Cucumber + Mocking

I know. Cucumber is used for integration tests. Integration tests and mocking shouldn't even be in the same sentence. However, there are times when practicality trumps theory.

For instance, emails. How do you ensure that emails have been sent/received?

Another example is MailChimp. When a user signs up, you add them to a MailChimp list. Now, whenever you need to create a user, your app hits MailChimp. Since you are suppose to start with a clean slate, you now need to clean out the MailChimp list. This gets tedious and time consuming.

To speed things up, you may want to mock certain tasks like these.

Cucumber supports RSpec mocking (aka doubles) pretty easily.

Open up features/support/local_env.rb (or your custom config file for Cucumber)
require 'cucumber/rspec/doubles'

Done! Enjoy!

(Just as a note, I would test MailChimp without mocking when testing signup. I would mock it out in all other cases. Unfortunately, I haven't figured out how to test emails for real. If you have any suggestions, please leave a comment. I would love to hear it!)

No comments:

Post a Comment