Skip to content

Instantly share code, notes, and snippets.

@jmaicher
jmaicher / gist:673974
Created November 12, 2010 10:56
Sign up feature with cucumber and rspec
Feature: Sign up
In order to use the provided service
As a Guest
I want to sign up for an user account
@jmaicher
jmaicher / gist:702025
Created November 16, 2010 16:32
Working with cucumber tables, factory_girl and email_steps
Feature: Verify email address
In order to ensure communication to the User
As a Service Provider
I want the User to verify his email address
Scenario: Verify email address after sign up
Given I sign up as an user with:
| email | johndoe@example.com |
Then "johndoe@example.com" should receive 1 email
When I open the email
@jmaicher
jmaicher / gist:721500
Created November 30, 2010 10:42
Problem using any_of criteria with :all
def self.find_by_string (query)
users = User.any_of({ :username => /^#{Regexp.escape(query)}/ }, { :name => /#{Regexp.escape(query)}/ }).first
end
# returns #<User _id: 4cf4d4594dd96225fe00002d, deleted_at: nil, created_at: 2010-11-30 10:39:21 UTC, updated_at: 2010-11-30 10:39:21 UTC, name: "John Doe", username: "johndoe_44" [..] >
def self.find_by_string (query)
users = User.any_of({ :username => /^#{Regexp.escape(query)}/ }, { :name => /#{Regexp.escape(query)}/ }).all
end
# returns #<Mongoid::Criteria:0x3e1c2d0 @selector={:deleted_at=>{"$exists"=>false}, "$or"=>[{:username=>/^johndoe_44/}, {:name=>/johndoe_44/}]}, @options={}, @klass=User, @documents=[]>
@jmaicher
jmaicher / gist:725120
Created December 2, 2010 10:45
Focus on working specs with tags and filters
# within spec_helpers.rb
RSpec.configure do |config|
config.filter_run :focus => true
end
# within specs
describe "GET 'some_action'", :focus => true do
it "should be successful" do
get :some_action
@jmaicher
jmaicher / gist:728152
Created December 4, 2010 12:36
Mongoid doesn't automatically cast strings to BSON::ObjectID
# this doesn't work, because username_or_id is a string either way
def self.find_by_username_or_id(username_or_id)
User.any_of({:username => username_or_id}, {:_id => username_or_id}).first
# raises "illegal ObjectID format" exception
end
@jmaicher
jmaicher / gist:728159
Created December 4, 2010 12:56
Testing attribute changes in the database with cucumber
Background:
Given I am signed in as a user
And I am on the dashboard site
Scenario: Change e-mail address
When I follow "Edit user account"
And I fill in "E-mail" with "new_email@example.com"
And I press "Update user account"
Then I should see a confirmation message
And my "E-mail" should be "new_email@example.com"
@jmaicher
jmaicher / gist:728441
Created December 4, 2010 20:03
Using Selenium with Cucumber and Fakeweb
# cucumber supports selenium by default, just mark features with @selenium tag
@selenium
Feature: This feature will be executed with selenium
...
@jmaicher
jmaicher / gist:728462
Created December 4, 2010 20:33
Confirm Alerts using Cucumber with Selenium
# create the following step to confirm the next alert
When /^(?:|I )follow "([^"]*)" and I confirm the next alert$/ do |link|
page.evaluate_script('window.confirm = function() { return true; }')
When "I follow \"#{link}\""
end
@jmaicher
jmaicher / gist:743238
Created December 16, 2010 09:43
DRY your specs - Example: Sign in user
# spec/support/controller_spec_helper.rb
module ControllerSpecHelper
def sign_in_user(user)
controller.stub!(:current_user).and_return user
end
end
# spec/spec_helper.rb
RSpec.configure do |config|
# [..]
steal.plugins("app/home");