Created
July 25, 2011 08:20
-
-
Save azuby/1103756 to your computer and use it in GitHub Desktop.
rspec testing of User controller
This file contains hidden or 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 | |
@attr = { :name => "Example User", | |
:email => "[email protected]", | |
:password => "secret" | |
#:password_confirmation => "secret" | |
} | |
end | |
it "should create a new instance given valid attributes" do | |
User.create!(@attr) | |
end | |
# Name validation tests | |
it "should require a name" do | |
no_name_user = User.new(@attr.merge(:name => "")) | |
no_name_user.should_not be_valid | |
end | |
it "should reject names that are too long" do | |
long_name = "a" * 51 | |
long_name_user = User.new(@attr.merge(:name => long_name)) | |
long_name_user.should_not be_valid | |
end | |
# Email validation tests | |
it "should require an email" do | |
no_email_user = User.new(@attr.merge(:email => "")) | |
no_email_user.should_not be_valid | |
end | |
it "should accept valid email addresses" do | |
addresses = %w[[email protected] [email protected] [email protected]] | |
addresses.each do |address| | |
valid_email_user = User.new(@attr.merge(:email => address)) | |
valid_email_user.should be_valid | |
end | |
end | |
it "should reject invalid email addresses" do | |
addresses = %w[user@foo,com user_at_foo.org example.user@foo.] | |
addresses.each do |address| | |
invalid_email_user = User.new(@attr.merge(:email => address)) | |
invalid_email_user.should_not be_valid | |
end | |
end | |
it "should reject duplicate email addresses independent of case" do | |
upcased_email = @attr[:email].upcase | |
User.create!(@attr.merge(:email => upcased_email)) | |
user_with_duplicate_email = User.new(@attr) | |
user_with_duplicate_email.should_not be_valid | |
end | |
# Password validation tests | |
describe "password validations" do | |
it "should require a password" do | |
User.new(@attr.merge(:password => "", :password_confirmation => "")).should_not be_valid | |
end | |
it "should require a matching password confirmation" do | |
User.new(@attr.merge(:password_confirmation => "invalid")).should_not be_valid | |
end | |
it "should reject short passwords" do | |
short = "a" * 5 | |
hash = @attr.merge(:password => short, :password_confirmation => short) | |
User.new(hash).should_not be_valid | |
end | |
it "should reject long passwords" do | |
long = "a" * 41 | |
hash = @attr.merge(:password => long, :password_confirmation => long) | |
User.new(hash).should_not be_valid | |
end | |
end | |
# Password encryption tests | |
describe "password encryption" do | |
before(:each) do | |
@user = User.new(@attr) | |
# @user = User.create!(@attr) | |
p @user.save | |
p @user | |
end | |
it "should have a password_digest attribute" do | |
@user.should respond_to(:password_digest) | |
end | |
it "should set the password digest" do | |
@user.password_digest.should_not be_blank | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment