Skip to content

Instantly share code, notes, and snippets.

@stonegao
Created November 8, 2010 05:34
Show Gist options
  • Select an option

  • Save stonegao/667416 to your computer and use it in GitHub Desktop.

Select an option

Save stonegao/667416 to your computer and use it in GitHub Desktop.
How did I configure Cucumber and Rspec to work with Mongoid and Factory_girl ( according to Kevin Faustino - 07.22.2010 )
RSpec
Here is a my RSpec configuration which will empty my MongoDB db and also use Factory Girl to create my models.
Note that config.user_transactional_fixtures is commented out.
# spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'remarkable/active_model'
require 'remarkable/mongoid'
require 'factory_girl'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, comment the following line or assign false
# instead of true.
# config.use_transactional_fixtures = true
config.before :each do
Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
end
end
Cucumber
I created a file features/support/hooks.rb that will drop my MongoDB collections before each scenario.
# features/support/hooks.rb
Before do |scenario|
Mongoid.master.collections.reject { |c| c.name == 'system.indexes'}.each(&:drop)
end
features/support/env.rb file which will import all my factories. It will also provide some already defined step definitions by you guys at thoughtbot.
I read about you in your post gimme three steps.
# env.rb
ENV["RAILS_ENV"] ||= "test"
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
require 'cucumber/formatter/unicode'
require 'cucumber/rails/rspec'
require 'cucumber/rails/world'
require 'cucumber/web/tableish'
require 'capybara/rails'
require 'capybara/cucumber'
require 'capybara/session'
require 'cucumber/rails/capybara_javascript_emulation'
Capybara.default_selector = :css
ActionController::Base.allow_rescue = false
require 'factory_girl'
require 'factory_girl/step_definitions'
Dir[File.expand_path(File.join(File.dirname(__FILE__),'..','..','spec','factories','*.rb'))].each {|f| require f}
All the step definitions from factory_girl/step_definitions work except for “create record & set one attribute”:
Given an author exists with an email of "[email protected]"
The reason for this is because in the Factory girl code, there is a check that determines if the given model responds to :columns.
To get around this, add the following code to a new file features/step_definitions/mongoid_steps.rb:
# features/step_definitions/mongoid_steps.rb
Given /^an (.+) exists with an? (.+) of "([^"]*)"$/ do |model, field, value|
Factory model, field => value
end
The last thing to do is stub out the rake task for db:test:prepare.
The Cucumber rake tasks are still dependent on the ActiveRecord test workflow and will attempt to load the db schema into the test db.
I create the  file lib/tasks/mongo.rake to get around this:
# lib/tasks/mongo.rake
namespace :db do
namespace :test do
task :prepare do
# Stub out for MongoDB
end
end
end
I created a factory file : /specs/factories/user.rb
# specs/factories/user.rb
Factory.define : minimal_user, :class => User do |u|
u.email '[email protected]'
u.password 'test1234'
u.password_confirmation 'test1234'
end
I created a feature file : /features/users/update_profile.feature
# features/users/update_profile.feature
Feature: Update user profile
In order to keep my profile up-to-date
As a user
I want to be able to update my profile
@wip
Scenario: Update minimal user profile successfully
Given a minimal_user exists
And I am on the edit user profile page
Then show me the paged # using launch gem
running cucumber in the console
$ rake cucumber:wip
….
Using the wip profile...
Feature: Update user profile
In order to keep my profile up-to-date
As a user
I want to be able to update my profile
@wip
Scenario: Update minimal user profile successfully # features/user/update_profile.feature:7
Given a minimal_user exists # features/user/update_profile.feature:8
1 scenario (1 undefined)
8 steps (7 skipped, 1 undefined)
0m0.085s
You can implement step definitions for undefined steps with these snippets:
Given /^a minimal_user exists$/ do
pending # express the regexp above with the code you wish you had
end
I thought the step definition was defined in Factory_girl/step_definitions ? or it's not loaded ? where am I wrong .. or missing some important point ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment