Testing Rails gem plugins using cucumber can be difficult since gems need to be tested in the context of a host Rails application, which isn’t part of your gem. Cukigem gives you cucumber steps to make testing your gem in the context of a Rails application easy.
This is a nice addition to gems as it’s more of an integration test - verifying that your gem actually works and is successfully-installed in a Rails application via the Gemfile.
This is not a gem but a collection of useful steps. Simply copy the step definition files located in step_definitions, and the support files located in support, into your own project.
See this example in use on my message_block project. All of the step definitions should be self-explanatory if you read the step_definitions/cukigem_steps.rb file.
Feature: Message Block
Scenario: Installing Static Files
Given I have a rails application
And I run "rake message_block:install --trace"
Then the file "public/images/message_block" should exist
And the file "public/stylesheets/message_block.css" should exist
And the file "public/javascripts/message_block.js" should exist
Scenario: Displaying Model Errors mixed with Flash Messages
Given I have a rails application
And I run "rails g scaffold model name:string"
And I setup the database
And I save the following as "app/controllers/models_controller.rb":
"""
class ModelsController < ApplicationController
def index
@model = Model.new
@model.errors.add(:base, "Model-One")
@model.errors.add(:base, "Model-Two")
flash.now[:error] = "Controller-One"
flash.now[:confirm] = "Controller-Two"
end
end
"""
And I save the following as "app/views/models/index.html.erb":
"""
<%= message_block :on => :model %>
"""
And I start the rails application
When I go to the models page
Then I should see "Model-One"
And I should see "Model-Two"
And I should see "Controller-One"
And I should see "Controller-Two"
-
Be careful when using multiple “generate a rails application” steps in one test run. The problem is that we can only require the config/environment.rb file once, so if you blow away and generate a new application in the course of the test run, things will not behave as expected. Cukigem does a lot to “reset” your app to a blank state so there should rarely be a need to fully generate a new application except if one doesn’t already exist.
I am really looking for contributions here. I created this after looking for a standard way of testing my gems using cucumber and I was unable to find anything (except for some nice code in the paperclip project which did most of what I needed). Patches/pull-requests welcome.
Thanks to Jon Yurek of Thoughtbot for getting me thinking about this after seeing his work on Rails app generation and cucumber within the paperclip project.