/spec/models/user_spec.rb
https://github.com/diegopacheco/rails3-tutorial-sample-app · Ruby · 276 lines · 252 code · 24 blank · 0 comment · 2 complexity · 92b27db75dcc2b7fae0479cca1dc1aff MD5 · raw file
- require 'spec_helper'
- describe User do
-
- before(:each) do
- @attr = {
- :name => "Example User",
- :email => "user@example.com",
- :password => "foobar",
- :password_confirmation => "foobar"
- }
- end
- it "1. should create a new instance given valid attributes" do
- User.create!(@attr)
- end
- it "2. should require a name" do
- no_name_user = User.new(@attr.merge(:name => ""))
- no_name_user.should_not be_valid
- end
- it "3. 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 "4. 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 "5. should reject invalid email addresses" do
- addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
- addresses.each do |address|
- valid_email_user = User.new(@attr.merge(:email => address))
- valid_email_user.should_not be_valid
- end
- end
- it "6. should reject duplicate email addresses" do
- User.create!(@attr)
- user_with_duplicate_email = User.new(@attr)
- user_with_duplicate_email.should_not be_valid
- end
- it "7. 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
- it "8. should require a email" do
- no_name_user = User.new(@attr.merge(:email => ""))
- no_name_user.should_not be_valid
- end
- describe "password validations" do
-
- it "9. should require a password" do
- User.new(@attr.merge(:password => "", :password_confirmation => "")).
- should_not be_valid
- end
-
- it "10. 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 "11. should reject long passwords" do
- long = "a" * 41
- hash = @attr.merge(:password => long, :password_confirmation => long)
- User.new(hash).should_not be_valid
- end
- it "12. should require a matching password confirmation" do
- User.new(@attr.merge(:password_confirmation => 'invalid')).
- should_not be_valid
- end
- end
- describe "password encryption" do
- before(:each) do
- @user = User.create!(@attr)
- end
- it "13. should have a encrypted password attribute" do
- @user.should respond_to(:encrypted_password)
- end
- it "14. should set the encrypted password" do
- @user.encrypted_password.should_not be_blank
- end
- describe "has_password? method" do
-
- it "15. should be true if passwords match" do
- @user.has_password?(@attr[:password]).should be_true
- end
- it "16. should be false if the passwords don't match" do
- @user.has_password?("invalid").should be_false
- end
- end
- describe "authenticate method" do
-
- it "17. should return nil on email/password mismatch" do
- wrong_password_user = User.authenticate(@attr[:email], "wrongpass")
- wrong_password_user.should be_nil
- end
- it "18. should return nil for an email addresses with no user_with_duplicate_email" do
- nonexistent_user = User.authenticate("bar@foo.com", @attr[:passwords])
- nonexistent_user.should be_nil
- end
- it "19. should return the user on email/password match" do
- matching_user = User.authenticate(@attr[:email],@attr[:password])
- matching_user == @user
- end
- end
- end
- describe "admin attribute" do
-
- before(:each) do
- @user = User.create!(@attr)
- end
-
- it "should respond to admin" do
- @user.should respond_to(:admin)
- end
- it "should not be an admin by default" do
- @user.should_not be_admin
- end
-
- it "should be convertible to an admin" do
- @user.toggle!(:admin)
- @user.should be_admin
- end
-
- end
- describe "micropost associations" do
-
- before(:each) do
- @user = User.create(@attr)
- @mp1 = Factory(:micropost, :user => @user, :created_at => 1.day.ago)
- @mp2 = Factory(:micropost, :user => @user, :created_at => 1.hour.ago)
- end
- it "should have a microposts attribute" do
- @user.should respond_to(:microposts)
- end
- it "should have the right microposts in the right order" do
- @user.microposts.should == [@mp2, @mp1]
- end
- it "should destroy associated microposts" do
- @user.destroy
- [@mp1,@mp2].each do |micropost|
- Micropost.find_by_id(micropost.id).should be_nil
- end
- end
- describe "status feed" do
- it "should have a feed" do
- @user.should respond_to(:feed)
- end
- it "should include the user's microposts" do
- @user.feed.include?(@mp1).should be_true
- @user.feed.include?(@mp2).should be_true
- end
- it "should not include a different user's microposts" do
- mp3 = Factory(:micropost,:user => Factory(:user,:email => Factory.next(:email)))
- @user.feed.include?(mp3).should be_false
- end
- it "should include the microposts of followed users" do
- followed = Factory(:user, :email => Factory.next(:email))
- mp3 = Factory(:micropost, :user => followed)
- @user.follow!(followed)
- @user.feed.should include(mp3)
- end
- end
- end
- describe "relationships" do
-
- before(:each) do
- @user = User.create!(@attr)
- @followed = Factory(:user)
- end
- it "should have a relationships method" do
- @user.should respond_to(:relationships)
- end
- it "should have a following method" do
- @user.should respond_to(:following)
- end
- it "should have a following? method" do
- @user.should respond_to(:following?)
- end
- it "should have a follow! method" do
- @user.should respond_to(:follow!)
- end
- it "should follow another user" do
- @user.follow!(@followed)
- @user.should be_following(@followed)
- end
- it "should include the followed user in the following array" do
- @user.follow!(@followed)
- @user.following.should include(@followed)
- end
-
- it "should have a nunfollow! method" do
- @followed.should respond_to(:unfollow!)
- end
- it "should unfollow a user" do
- @user.follow!(@followed)
- @user.unfollow!(@followed)
- @user.should_not be_following(@followed)
- end
- it "should have a reverse_relationships method" do
- @user.should respond_to(:reverse_relationships)
- end
-
- it "should have a followers method" do
- @user.should respond_to(:followers)
- end
- it "should include the follower in the followers array" do
- @user.follow!(@followed)
- @followed.followers.should include(@user)
- end
- end
-
- end
- # == Schema Information
- #
- # Table name: users
- #
- # id :integer not null, primary key
- # name :string(255)
- # email :string(255)
- # created_at :datetime
- # updated_at :datetime
- #