Created
March 5, 2012 17:04
-
-
Save Will-Sommers/1979347 to your computer and use it in GitHub Desktop.
Rspec remember_token
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
$ rails console | |
Loading development environment (Rails 3.2.1) | |
1.9.3-p0 :001 > User.first.remember_token | |
User Load (0.6ms) SELECT "users".* FROM "users" LIMIT 1 | |
NoMethodError: undefined method `remember_token' for #<User:0x007fec96cf9830> | |
from /Users/Will/.rvm/gems/ruby-1.9.3-p0@rails313/gems/activemodel-3.2.1/lib/active_model/attribute_methods.rb:407:in `method_missing' | |
from /Users/Will/.rvm/gems/ruby-1.9.3-p0@rails313/gems/activerecord-3.2.1/lib/active_record/attribute_methods.rb:126:in `method_missing' | |
from (irb):1 | |
from /Users/Will/.rvm/gems/ruby-1.9.3-p0@rails313/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start' | |
from /Users/Will/.rvm/gems/ruby-1.9.3-p0@rails313/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start' | |
from /Users/Will/.rvm/gems/ruby-1.9.3-p0@rails313/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>' | |
from script/rails:6:in `require' | |
from script/rails:6:in `<main>' | |
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
class AddRememberTokenToUsers < ActiveRecord::Migration | |
def change | |
add_column :users, :remember_token, :string | |
add_index :users, :remember_token | |
end |
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
ActiveRecord::Schema.define(:version => 20120305161041) do | |
create_table "users", :force => true do |t| | |
t.string "name" | |
t.string "email" | |
t.datetime "created_at", :null => false | |
t.datetime "updated_at", :null => false | |
t.string "password_digest" | |
end | |
add_index "users", ["email"], :name => "index_users_on_email", :unique => true | |
end |
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
class User < ActiveRecord::Base | |
attr_accessible :name, :email, :password, :password_confirmation | |
has_secure_password | |
before_save :create_remember_token | |
validates :name, presence: true, length: { maximum: 50 } | |
valid_email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i | |
validates :email, presence: true, | |
format: { with: valid_email_regex }, | |
uniqueness: { case_sensitive: false } | |
validates :password, length: { minimum: 6 } | |
private | |
def create_remember_token | |
self.remember_token = SecureRandom.urlsafe_base64 | |
end | |
end |
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 do | |
@user = User.new(name: "Example User", email: "[email protected]", | |
password: "foobar", password_confirmation: "foobar") | |
end | |
subject { @user } | |
it { should respond_to(:name) } | |
it { should respond_to(:email) } | |
it { should respond_to(:password_digest) } | |
it { should respond_to(:password ) } | |
it { should respond_to(:password_confirmation) } | |
it { should respond_to(:remember_token ) } | |
it { should respond_to(:authenticate ) } | |
it { should be_valid } | |
it { should respond_to(:authenticate) } | |
describe "when name is not present" do | |
before { @user.name = " " } | |
it { should_not be_valid } | |
end | |
describe "when email is not present" do | |
before { @user.email = " " } | |
it { should_not be_valid } | |
end | |
describe "when name is too long" do | |
before { @user.name = "a" * 51 } | |
it { should_not be_valid } | |
end | |
describe "when email format is invalid" do | |
invalid_addresses = %w[user@foo,com user_at_foo.org example.user@foo.] | |
invalid_addresses.each do |invalid_address| | |
before { @user.email = invalid_address } | |
it { should_not be_valid } | |
end | |
end | |
describe "when email format is valid" do | |
valid_addresses = %w[[email protected] [email protected] [email protected] [email protected]] | |
valid_addresses.each do |valid_address| | |
before { @user.email = valid_address } | |
it { should be_valid } | |
end | |
end | |
describe "when email address is already taken" do | |
before do | |
user_with_same_email = @user.dup | |
user_with_same_email.email = @user.email.upcase | |
user_with_same_email.save | |
end | |
it { should_not be_valid } | |
end | |
describe "when password is not present" do | |
before { @user.password = @user.password_confirmation = " " } | |
it { should_not be_valid } | |
end | |
describe "when password doesn't match confirmation" do | |
before { @user.password_confirmation = "mismatch" } | |
it { should_not be_valid } | |
end | |
describe "return value of authenticate method" do | |
before { @user.save } | |
let(:found_user) { User.find_by_email(@user.email) } | |
describe "with valid password" do | |
it { should == found_user.authenticate(@user.password) } | |
end | |
describe "with invalid password" do | |
let(:user_for_invalid_password) { found_user.authenticate("invalid") } | |
it { should_not == user_for_invalid_password } | |
specify { user_for_invalid_password.should be_false } | |
end | |
end | |
describe "with a password that's too short" do | |
before { @user.password = @user.password_confirmation = "a" * 5 } | |
it { should be_invalid } | |
end | |
describe "remember token" do | |
before { @user.save } | |
its(:remember_token) { should_not be_blank } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment