Skip to content

Instantly share code, notes, and snippets.

@andrewhl
Created October 20, 2011 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrewhl/1301642 to your computer and use it in GitHub Desktop.
Save andrewhl/1301642 to your computer and use it in GitHub Desktop.
Running: spec/models/user_spec.rb
FFFFFFFFFFFFFF
Failures:
1) User should create a new instance given valid attributes
Failure/Error: User.create!(@attr)
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:14:in `block (2 levels) in <top (required)>'
2) User should require a name
Failure/Error: no_name_user = User.new(@attr.merge(:name => ""))
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:18:in `new'
# ./spec/models/user_spec.rb:18:in `block (2 levels) in <top (required)>'
3) User should require an email address
Failure/Error: no_email_user = User.new(@attr.merge(:email => ""))
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:23:in `new'
# ./spec/models/user_spec.rb:23:in `block (2 levels) in <top (required)>'
4) User should reject names that are too long
Failure/Error: long_name_user = User.new(@attr.merge(:name => long_name))
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:29:in `new'
# ./spec/models/user_spec.rb:29:in `block (2 levels) in <top (required)>'
5) User should accept valid email addresses
Failure/Error: valid_email_user = User.new(@attr.merge(:email => address))
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:36:in `new'
# ./spec/models/user_spec.rb:36:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:35:in `each'
# ./spec/models/user_spec.rb:35:in `block (2 levels) in <top (required)>'
6) User should reject invalid email addresses
Failure/Error: invalid_email_user = User.new(@attr.merge(:email => address))
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:44:in `new'
# ./spec/models/user_spec.rb:44:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:43:in `each'
# ./spec/models/user_spec.rb:43:in `block (2 levels) in <top (required)>'
7) User should reject duplicate email addresses
Failure/Error: User.create!(@attr)
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:51:in `block (2 levels) in <top (required)>'
8) User should reject email addresses identical up to case
Failure/Error: User.create!(@attr.merge(:email => upcased_email))
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:58:in `block (2 levels) in <top (required)>'
9) User password validations should require a password
Failure/Error: User.new(@attr.merge(:password => "", :password_confirmation => "")).should_not be_valid
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:67:in `new'
# ./spec/models/user_spec.rb:67:in `block (3 levels) in <top (required)>'
10) User password validations should require a matching password confirmation
Failure/Error: User.new(@attr.merge(:password_confirmation => "invalid")).should_not be_valid
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:71:in `new'
# ./spec/models/user_spec.rb:71:in `block (3 levels) in <top (required)>'
11) User password validations should reject short passwords
Failure/Error: User.new(hash).should_not be_valid
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:77:in `new'
# ./spec/models/user_spec.rb:77:in `block (3 levels) in <top (required)>'
12) User password validations should reject long password
Failure/Error: User.new(hash).should_not be_valid
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:83:in `new'
# ./spec/models/user_spec.rb:83:in `block (3 levels) in <top (required)>'
13) User password encryption should have an encrypted password attribute
Failure/Error: @user = User.create!(@attr)
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:90:in `block (3 levels) in <top (required)>'
14) User password encryption should set the encrypted password
Failure/Error: @user = User.create!(@attr)
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:90:in `block (3 levels) in <top (required)>'
Finished in 0.04037 seconds
14 examples, 14 failures
Failed examples:
rspec ./spec/models/user_spec.rb:13 # User should create a new instance given valid attributes
rspec ./spec/models/user_spec.rb:17 # User should require a name
rspec ./spec/models/user_spec.rb:22 # User should require an email address
rspec ./spec/models/user_spec.rb:27 # User should reject names that are too long
rspec ./spec/models/user_spec.rb:33 # User should accept valid email addresses
rspec ./spec/models/user_spec.rb:41 # User should reject invalid email addresses
rspec ./spec/models/user_spec.rb:49 # User should reject duplicate email addresses
rspec ./spec/models/user_spec.rb:56 # User should reject email addresses identical up to case
rspec ./spec/models/user_spec.rb:66 # User password validations should require a password
rspec ./spec/models/user_spec.rb:70 # User password validations should require a matching password confirmation
rspec ./spec/models/user_spec.rb:74 # User password validations should reject short passwords
rspec ./spec/models/user_spec.rb:80 # User password validations should reject long password
rspec ./spec/models/user_spec.rb:93 # User password encryption should have an encrypted password attribute
rspec ./spec/models/user_spec.rb:97 # User password encryption should set the encrypted password
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
# Automatically create the virtual attribute 'password_confirmation'.
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..40 }
before_save :encrypt_password
private
def encrypt_password
self.encrypted_password = encrypt(password)
end
def encrypt(string)
string # Only a temporary implementation!
end
end
require 'spec_helper'
describe User do
before(:each) do
@attr = {
:name => "Example User",
:email => "user@example.com",
:password => "foobar",
:password_confirmation => "foobar"
}
end
it "should create a new instance given valid attributes" do
User.create!(@attr)
end
it "should require a name" do
no_name_user = User.new(@attr.merge(:name => ""))
no_name_user.should_not be_valid
end
it "should require an email address" do
no_email_user = User.new(@attr.merge(:email => ""))
no_email_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
it "should accept valid email addresses" do
addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
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" do
# Put a user with given email address into the database.
User.create!(@attr)
user_with_duplicate_email = User.new(@attr)
user_with_duplicate_email.should_not be_valid
end
it "should reject email addresses identical up to 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
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 password" do
long = "a" * 41
hash = @attr.merge(:password => long, :password_confirmation => long)
User.new(hash).should_not be_valid
end
end
describe "password encryption" do
before(:each) do
@user = User.create!(@attr)
end
it "should have an encrypted password attribute" do
@user.should respond_to(:encrypted_password)
end
it "should set the encrypted password" do
@user.encrypted_password.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