Created
November 12, 2010 10:56
-
-
Save jmaicher/673974 to your computer and use it in GitHub Desktop.
Sign up feature with cucumber and rspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: Sign up | |
In order to use the provided service | |
As a Guest | |
I want to sign up for an user account |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe User do | |
before :each do | |
@user = Factory.create(:user) | |
end | |
it "should be invalid without an email address" do | |
@user.email = nil | |
@user.should_not be_valid | |
end | |
it "should have an unique email address" do | |
new_user = Factory.build(:user) | |
new_user.email = @user.email | |
new_user.should_not be_valid | |
end | |
it "should be invalid with an invalid email address" do | |
invalid_email_addresses.each do |invalid_email| | |
@user.email = invalid_email | |
@user.should_not be_valid | |
end | |
end | |
it "should be valid with an valid email address" do | |
valid_email_addresses.each do |valid_email| | |
@user.email = valid_email | |
@user.should be_valid | |
end | |
end | |
# generate array of valid email addresses | |
def invalid_email_addresses | |
["mail@example", "mail", "@example.com", "mailexample.com", "m [email protected]", "mail@ex ample.com", "[email protected]"] | |
end | |
def valid_email_addresses | |
["[email protected]", "[email protected]", "[email protected]", "[email protected]"] | |
end | |
# [..] | |
end # describe User |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
include Mongoid::Document | |
# [..] | |
field :email, :type => String | |
validates :email, :presence => true, | |
:uniqueness => true, | |
:length => {:minimum => 6}, | |
:format => {:with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i} | |
# [..] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe "POST 'create'" do | |
before :each do | |
@user = mock_model(User, :save => true) | |
User.stub!(:new).and_return @user | |
end | |
def do_post | |
post :create, :user => post_parameter | |
end | |
def post_parameter | |
['key' => 'value'] | |
end | |
it "should create a user object from the given parameter" do | |
User.should_receive(:new).with post_parameter | |
do_post | |
end | |
end # POST 'create' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe "POST 'create'", "with valid user data" do | |
before :each do | |
@user = mock_model(User, :save => true) | |
User.stub!(:new).and_return @user | |
end | |
it "should save the new user object" do | |
@user.should_receive(:save).and_return true | |
post :create | |
end | |
it "should redirect to homepage" do | |
post :create | |
response.should redirect_to(root_path) | |
end | |
it "should assign a flash success message" do | |
post :create | |
flash[:success].should_not be_blank | |
end | |
end # POST 'create' with valid user data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In order to <benefit> | |
As a <role> | |
I want <something> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe "POST 'create'", "with invalid user data" do | |
before :each do | |
@user = mock_model(User, :save => false) | |
User.stub!(:new).and_return @user | |
end | |
it "should not save the new user object" do | |
@user.should_receive(:save).and_return false | |
post :create | |
end | |
it "should redirect to signup page" do | |
post :create | |
response.should render_template :signup | |
end | |
it "should assign a flash error message" do | |
post :create | |
flash[:error].should_not be_blank | |
end | |
end # POST 'create' with invalid user data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
# [..] | |
# POST /users | |
def create | |
@user = User.new(params[:user]) | |
if @user.save | |
flash[:success] = 'Sign up was successful' | |
redirect_to(root_path) | |
else | |
render :action => "signup" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Background: | |
Given I am on the sign up page | |
Scenario: Sign up with valid data | |
When I fill in the following: | |
| Name | John Doe | | |
| Username | johndoe | | |
| Email | [email protected] | | |
| Password | secret | | |
| Password confirmation | secret | | |
And I press "Sign up" | |
Then I should see a confirmation message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Scenario: Sign up with invalid data | |
When I fill in the following: | |
| Name | John Doe | | |
| Username | johndoe | | |
| Email | invalid-email-address | | |
| Password | secret | | |
| Password confirmation | secret | | |
And I press "Sign up" | |
Then I should see an error message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment