From 1f463d715cc0c1a816394570e39c3ac68260eaa1 Mon Sep 17 00:00:00 2001 From: Jacques Crocker Date: Fri, 30 Jul 2010 17:02:29 -0700 Subject: [PATCH] Adding Pickle, and rearranging cucumber support/ folder a bit --- Gemfile | 2 + Gemfile.lock | 11 +++ features/step_definitions/pickle_steps.rb | 100 ++++++++++++++++++++++ features/support/cleaner.rb | 4 + features/support/env.rb | 9 +- features/support/factory_girl.rb | 2 + features/support/pickle.rb | 24 ++++++ 7 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 features/step_definitions/pickle_steps.rb create mode 100644 features/support/cleaner.rb create mode 100644 features/support/factory_girl.rb create mode 100644 features/support/pickle.rb diff --git a/Gemfile b/Gemfile index 3295b1f5..5f322536 100644 --- a/Gemfile +++ b/Gemfile @@ -43,6 +43,8 @@ group :test do gem "autotest" gem 'rspec-rails', '2.0.0.beta.19' gem 'factory_girl_rails' + gem "pickle", :git => "http://github.com/codegram/pickle.git" + gem "pickle-mongoid" gem 'capybara' gem "capybara-envjs", ">= 0.1.5" gem 'database_cleaner' diff --git a/Gemfile.lock b/Gemfile.lock index a7c594d6..e29f01a8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -24,6 +24,12 @@ GIT bcrypt-ruby (~> 2.1.2) warden (~> 0.10.7) +GIT + remote: http://github.com/codegram/pickle.git + revision: 2834204 + specs: + pickle (0.3.0) + GIT remote: http://github.com/justinfrench/formtastic.git revision: 4481341 @@ -165,6 +171,9 @@ GEM fastthread (>= 1.0.1) gem_plugin (>= 0.2.3) nokogiri (1.4.3.1) + pickle-mongoid (0.1.6) + mongoid (>= 2.0.0.beta.7) + pickle (>= 0.3.0) polyglot (0.3.1) rack (1.2.1) rack-mount (0.6.9) @@ -263,6 +272,8 @@ DEPENDENCIES mongoid! mongoid_acts_as_tree (= 0.1.5) mongrel + pickle! + pickle-mongoid rails (= 3.0.0.rc) rmagick (= 2.12.2) rspec-rails (= 2.0.0.beta.19) diff --git a/features/step_definitions/pickle_steps.rb b/features/step_definitions/pickle_steps.rb new file mode 100644 index 00000000..756c073f --- /dev/null +++ b/features/step_definitions/pickle_steps.rb @@ -0,0 +1,100 @@ +# this file generated by script/generate pickle + +# create a model +Given(/^#{capture_model} exists?(?: with #{capture_fields})?$/) do |name, fields| + create_model(name, fields) +end + +# create n models +Given(/^(\d+) #{capture_plural_factory} exist(?: with #{capture_fields})?$/) do |count, plural_factory, fields| + count.to_i.times { create_model(plural_factory.singularize, fields) } +end + +# create models from a table +Given(/^the following #{capture_plural_factory} exists?:?$/) do |plural_factory, table| + create_models_from_table(plural_factory, table) +end + +# find a model +Then(/^#{capture_model} should exist(?: with #{capture_fields})?$/) do |name, fields| + find_model!(name, fields) +end + +# not find a model +Then(/^#{capture_model} should not exist(?: with #{capture_fields})?$/) do |name, fields| + find_model(name, fields).should be_nil +end + +# find models with a table +Then(/^the following #{capture_plural_factory} should exists?:?$/) do |plural_factory, table| + find_models_from_table(plural_factory, table).should_not be_any(&:nil?) +end + +# find exactly n models +Then(/^(\d+) #{capture_plural_factory} should exist(?: with #{capture_fields})?$/) do |count, plural_factory, fields| + find_models(plural_factory.singularize, fields).size.should == count.to_i +end + +# assert equality of models +Then(/^#{capture_model} should be #{capture_model}$/) do |a, b| + model!(a).should == model!(b) +end + +# assert model is in another model's has_many assoc +Then(/^#{capture_model} should be (?:in|one of|amongst) #{capture_model}(?:'s)? (\w+)$/) do |target, owner, association| + model!(owner).send(association).should include(model!(target)) +end + +# assert model is not in another model's has_many assoc +Then(/^#{capture_model} should not be (?:in|one of|amongst) #{capture_model}(?:'s)? (\w+)$/) do |target, owner, association| + model!(owner).send(association).should_not include(model!(target)) +end + +# assert model is another model's has_one/belongs_to assoc +Then(/^#{capture_model} should be #{capture_model}(?:'s)? (\w+)$/) do |target, owner, association| + model!(owner).send(association).should == model!(target) +end + +# assert model is not another model's has_one/belongs_to assoc +Then(/^#{capture_model} should not be #{capture_model}(?:'s)? (\w+)$/) do |target, owner, association| + model!(owner).send(association).should_not == model!(target) +end + +# assert model.predicate? +Then(/^#{capture_model} should (?:be|have) (?:an? )?#{capture_predicate}$/) do |name, predicate| + if model!(name).respond_to?("has_#{predicate.gsub(' ', '_')}") + model!(name).should send("have_#{predicate.gsub(' ', '_')}") + else + model!(name).should send("be_#{predicate.gsub(' ', '_')}") + end +end + +# assert not model.predicate? +Then(/^#{capture_model} should not (?:be|have) (?:an? )?#{capture_predicate}$/) do |name, predicate| + if model!(name).respond_to?("has_#{predicate.gsub(' ', '_')}") + model!(name).should_not send("have_#{predicate.gsub(' ', '_')}") + else + model!(name).should_not send("be_#{predicate.gsub(' ', '_')}") + end +end + +# model.attribute.should eql(value) +# model.attribute.should_not eql(value) +Then(/^#{capture_model}'s (\w+) (should(?: not)?) be #{capture_value}$/) do |name, attribute, expectation, expected| + actual_value = model(name).send(attribute) + expectation = expectation.gsub(' ', '_') + + case expected + when 'nil', 'true', 'false' + actual_value.send(expectation, send("be_#{expected}")) + when /^[+-]?[0-9_]+(\.\d+)?$/ + actual_value.send(expectation, eql(expected.to_f)) + else + actual_value.to_s.send(expectation, eql(eval(expected))) + end +end + +# assert size of association +Then /^#{capture_model} should have (\d+) (\w+)$/ do |name, size, association| + model!(name).send(association).size.should == size.to_i +end diff --git a/features/support/cleaner.rb b/features/support/cleaner.rb new file mode 100644 index 00000000..b210de64 --- /dev/null +++ b/features/support/cleaner.rb @@ -0,0 +1,4 @@ +require 'database_cleaner' +require 'database_cleaner/cucumber' +DatabaseCleaner.strategy = :truncation +DatabaseCleaner.orm = "mongoid" \ No newline at end of file diff --git a/features/support/env.rb b/features/support/env.rb index ce6051cf..cca05079 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -36,15 +36,8 @@ Capybara.javascript_driver = :envjs # of your scenarios, as this makes it hard to discover errors in your application. ActionController::Base.allow_rescue = false -require 'factory_girl' - Locomotive.configure do |config| config.default_domain = 'example.com' end -Capybara.default_host = 'test.example.com' - -require 'database_cleaner' -require 'database_cleaner/cucumber' -DatabaseCleaner.strategy = :truncation -DatabaseCleaner.orm = "mongoid" \ No newline at end of file +Capybara.default_host = 'test.example.com' \ No newline at end of file diff --git a/features/support/factory_girl.rb b/features/support/factory_girl.rb new file mode 100644 index 00000000..9f939212 --- /dev/null +++ b/features/support/factory_girl.rb @@ -0,0 +1,2 @@ +require 'factory_girl' + diff --git a/features/support/pickle.rb b/features/support/pickle.rb new file mode 100644 index 00000000..c8660e77 --- /dev/null +++ b/features/support/pickle.rb @@ -0,0 +1,24 @@ +# this file generated by script/generate pickle [paths] [email] +# +# Make sure that you are loading your factory of choice in your cucumber environment +# +# For machinist add: features/support/machinist.rb +# +# require 'machinist/active_record' # or your chosen adaptor +# require File.dirname(__FILE__) + '/../../spec/blueprints' # or wherever your blueprints are +# Before { Sham.reset } # to reset Sham's seed between scenarios so each run has same random sequences +# +# For FactoryGirl add: features/support/factory_girl.rb +# +# require 'factory_girl' +# require File.dirname(__FILE__) + '/../../spec/factories' # or wherever your factories are +# +# You may also need to add gem dependencies on your factory of choice in config/environments/cucumber.rb + +require 'pickle/world' +# Example of configuring pickle: +# +Pickle.configure do |config| + config.adapters = [:factory_girl] + # config.map 'I', 'myself', 'me', 'my', :to => 'user: "me"' +end