Skip to content

Commit

Permalink
Full migration to RSpec 3
Browse files Browse the repository at this point in the history
  • Loading branch information
chewi committed Aug 9, 2014
1 parent 3ad3a0a commit 2a10e31
Show file tree
Hide file tree
Showing 19 changed files with 393 additions and 324 deletions.
1 change: 1 addition & 0 deletions .rspec
@@ -1 +1,2 @@
--color
--require spec_helper
2 changes: 1 addition & 1 deletion Rakefile.d/cucumber.rake
Expand Up @@ -18,7 +18,7 @@ namespace :cucumber do
rm_rf "cucumber_test_app"
sh "rails new cucumber_test_app --skip-javascript --skip-sprockets"
sh "echo 'gem \"cucumber-rails\", :require => false' >> #{gemfile}"
sh "echo 'gem \"rspec-rails\"' >> #{gemfile}"
sh "echo 'gem \"rspec-rails\", \"~>3.0\"' >> #{gemfile}"
sh "echo 'gem \"capybara\"' >> #{gemfile}"
sh "bundle install --gemfile=#{gemfile}"
sh "ln -s ../../.. cucumber_test_app/vendor/plugins/pickle"
Expand Down
4 changes: 2 additions & 2 deletions features/step_definitions/fork_steps.rb
@@ -1,4 +1,4 @@
# example of making your own matcher with the pickle backend
Then(/^#{capture_model} should be tine of #{capture_model}$/) do |tine, fork|
model(fork).tines.should include(model(tine))
end
expect(model(fork).tines).to include(model(tine))
end
10 changes: 5 additions & 5 deletions features/step_definitions/generator_steps.rb
Expand Up @@ -32,21 +32,21 @@
end

Then(/^I should see "(.*)"$/) do |text|
@output.should include(text)
expect(@output).to include(text)
end

Then(/^the file (.+?) should exist$/) do |file|
File.exist?("#{Rails.root}/#{file}").should == true
expect(File.exist?("#{Rails.root}/#{file}")).to eq(true)
end

Then(/^the file (.+?) should match \/(.*?)\/$/) do |file, regexp|
File.read("#{Rails.root}/#{file}").should match(/#{regexp}/m)
expect(File.read("#{Rails.root}/#{file}")).to match(/#{regexp}/m)
end

Then(/^the file (.+?) should not match \/(.*?)\/$/) do |file, regexp|
File.read("#{Rails.root}/#{file}").should_not match(/#{regexp}/m)
expect(File.read("#{Rails.root}/#{file}")).not_to match(/#{regexp}/m)
end

Then /^the file ([^ ]+) should be identical to the local (.+)$/ do |generated_file, source_file|
File.read("#{Rails.root}/#{generated_file}").should == File.read("#{File.dirname(__FILE__)}/../#{source_file}")
expect(File.read("#{Rails.root}/#{generated_file}")).to eq(File.read("#{File.dirname(__FILE__)}/../#{source_file}"))
end
6 changes: 3 additions & 3 deletions features/step_definitions/path_steps.rb
Expand Up @@ -2,13 +2,13 @@

Then(/^(.+?) should match route \/(.+?)$/) do |page, route|
regexp = route.gsub(/:(\w*?)id/,'\d+')
path_to(page).should =~ /#{regexp}/
expect(path_to(page)).to match(/#{regexp}/)
end

When(/^I go to (.+)$/) do |page|
visit path_to(page)
end

Then(/^I should be at (.+)$/) do |page|
current_url.should =~ /#{path_to(page)}/
end
expect(current_url).to match(/#{path_to(page)}/)
end
6 changes: 3 additions & 3 deletions features/step_definitions/raise_error_steps.rb
@@ -1,7 +1,7 @@
Then /^the following should raise an? ([\w:]+):$/ do |error, step|
lambda { steps step }.should raise_error(error.constantize)
expect { steps step }.to raise_error(error.constantize)
end

Then /^the following should raise an? ([\w:]+) with "([^"]*)":$/ do |error, message, step|
lambda { steps step }.should raise_error(error.constantize, message)
end
expect { steps step }.to raise_error(error.constantize, message)
end
2 changes: 1 addition & 1 deletion pickle.gemspec
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "bundler"
s.add_development_dependency "git"
s.add_development_dependency "yard"
s.add_development_dependency "rspec-rails", "~>2.6.0"
s.add_development_dependency "rspec-rails", "~>3.0"
s.add_development_dependency "rails", "~>3.1.0"
s.add_development_dependency "cucumber-rails"
s.add_development_dependency "factory_girl"
Expand Down
20 changes: 10 additions & 10 deletions rails_generators/pickle/templates/email_steps.rb
Expand Up @@ -15,7 +15,7 @@
end

Given(/^(\d)+ emails? should be delivered$/) do |count|
emails.size.should == count.to_i
expect(emails.size).to eq(count.to_i)
end

When(/^(?:I|they) follow "([^"]*?)" in #{capture_email}$/) do |link, email_ref|
Expand All @@ -27,37 +27,37 @@
end

Then(/^(\d)+ emails? should be delivered to (.*)$/) do |count, to|
emails("to: \"#{email_for(to)}\"").size.should == count.to_i
expect(emails("to: \"#{email_for(to)}\"").size).to eq(count.to_i)
end

Then(/^(\d)+ emails? should be delivered with #{capture_fields}$/) do |count, fields|
emails(fields).size.should == count.to_i
expect(emails(fields).size).to eq(count.to_i)
end

Then(/^#{capture_email} should be delivered to (.+)$/) do |email_ref, to|
email(email_ref, "to: \"#{email_for(to)}\"").should_not be_nil
expect(email(email_ref, "to: \"#{email_for(to)}\"")).not_to be_nil
end

Then(/^#{capture_email} should not be delivered to (.+)$/) do |email_ref, to|
email(email_ref, "to: \"#{email_for(to)}\"").should be_nil
expect(email(email_ref, "to: \"#{email_for(to)}\"")).to be_nil
end

Then(/^#{capture_email} should have #{capture_fields}$/) do |email_ref, fields|
email(email_ref, fields).should_not be_nil
expect(email(email_ref, fields)).not_to be_nil
end

Then(/^#{capture_email} should contain "(.*)"$/) do |email_ref, text|
email(email_ref).body.should =~ /#{text}/
expect(email(email_ref).body).to match(/#{text}/)
end

Then(/^#{capture_email} should not contain "(.*)"$/) do |email_ref, text|
email(email_ref).body.should_not =~ /#{text}/
expect(email(email_ref).body).not_to match(/#{text}/)
end

Then(/^#{capture_email} should link to (.+)$/) do |email_ref, page|
email(email_ref).body.should =~ /#{path_to(page)}/
expect(email(email_ref).body).to match(/#{path_to(page)}/)
end

Then(/^show me the emails?$/) do
save_and_open_emails
end
end
38 changes: 19 additions & 19 deletions rails_generators/pickle/templates/pickle_steps.rb
Expand Up @@ -22,79 +22,79 @@

# not find a model
Then(/^#{capture_model} should not exist(?: with #{capture_fields})?$/) do |name, fields|
find_model(name, fields).should be_nil
expect(find_model(name, fields)).to 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?)
expect(find_models_from_table(plural_factory, table)).not_to 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
expect(find_models(plural_factory.singularize, fields).size).to eq(count.to_i)
end

# assert equality of models
Then(/^#{capture_model} should be #{capture_model}$/) do |a, b|
model!(a).should == model!(b)
expect(model!(a)).to eq(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))
expect(model!(owner).send(association)).to 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))
expect(model!(owner).send(association)).not_to 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)
expect(model!(owner).send(association)).to eq(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)
expect(model!(owner).send(association)).not_to eq(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(' ', '_')}")
expect(model!(name)).to send("have_#{predicate.gsub(' ', '_')}")
else
model!(name).should send("be_#{predicate.gsub(' ', '_')}")
expect(model!(name)).to 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(' ', '_')}")
expect(model!(name)).not_to send("have_#{predicate.gsub(' ', '_')}")
else
model!(name).should_not send("be_#{predicate.gsub(' ', '_')}")
expect(model!(name)).not_to send("be_#{predicate.gsub(' ', '_')}")
end
end

# model.attribute.should eql(value)
# model.attribute.should_not eql(value)
# expect(model.attribute).to eq(value)
# expect(model.attribute).not_to eq(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(' ', '_')
expectation = expectation.gsub("should", "to").gsub(" ", "_")

case expected
when 'nil', 'true', 'false'
actual_value.send(expectation, send("be_#{expected}"))
expect(actual_value).send(expectation, eq(eval(expected)))
when /^[+-]?[0-9_]+(\.\d+)?$/
actual_value.send(expectation, eql(expected.to_f))
expect(actual_value).send(expectation, eq(expected.to_f))
else
actual_value.to_s.send(expectation, eql(eval(expected)))
expect(actual_value.to_s).send(expectation, eq(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
expect(model!(name).send(association).size).to eq(size.to_i)
end

0 comments on commit 2a10e31

Please sign in to comment.