Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## user.rb
- class User < ActiveRecord::Base
- validates_presence_of :username
- validates_presence_of :email
- validates_as_email :email
- validates_format_of :username, :with => /^[A-Za-z][\w_]{2,}$/
- validates_uniqueness_of :username, :message => "That username is already taken. Please try selecting another one"
- validates_uniqueness_of :email, :message => "That email is already signed up"
- end
- ## user_spec.rb
- require File.dirname(__FILE__) '/../spec_helper'
- module UserSpecHelper
- def valid_user_attributes
- {
- :email => 'joe@bloggs.com',
- :username => 'joebloggs',
- :password => 'abcdefg'
- }
- end
- end
- context "A user (in general)" do
- fixtures :users
- include UserSpecHelper
- setup do
- @user = User.new
- end
- specify "should be invalid without a username" do
- @user.attributes = valid_user_attributes.except(:username)
- @user.should_not_be_valid
- @user.username = 'username'
- @user.should_be_valid
- end
- specify "should be invalid without an email address" do
- @user.attributes = valid_user_attributes.except(:email)
- @user.should_not_be_valid
- @user.email = "bob@bob.com"
- @user.should_be_valid
- end
- specify "should be invalid with a bad email address" do
- @user.attributes = valid_user_attributes.except(:email)
- @user.should_not_be_valid
- @user.email = "@bob.com"
- @user.should_not_be_valid
- @user.email = "Max@job 3:14"
- @user.should_not_be_valid
- @user.email = "bob@bob.com"
- @user.should_be_valid
- end
- specify "should be invalid with a username clash" do
- @user.attributes = valid_user_attributes.except(:username)
- @user.username = "dave"
- @user.should_not_be_valid
- @user.errors.on(:username).should_match /That username is already taken. Please try selecting another one/
- @user.username = "unique"
- @user.should_be_valid
- end
- specify "should be invalid with an email address clash" do
- @user.attributes = valid_user_attributes.except(:email)
- @user.email = "dave@bob.com"
- @user.should_not_be_valid
- @user.errors.on(:email).should_match /That email is already signed up/
- @user.email = "me@you.com"
- @user.should_be_valid
- end
- specify "should be invalid with illegal characters in username" do
- @user.attributes = valid_user_attributes.except(:username)
- @user.username = "da"
- @user.should_not_be_valid
- @user.username = "dave thomas"
- @user.should_not_be_valid
- @user.username = "bob-woodward"
- @user.should_not_be_valid
- @user.username = "_dopefiend"
- @user.should_not_be_valid
- @user.username = "bob_lee"
- @user.should_be_valid
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement