Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'spec_helper'
- describe User do
- before :each do
- @user = Factory :user, password: 'passwd'
- end
- context "#authenticate" do
- it 'should find user' do
- User.find_by_username(@user.username).try(:authenticate, @user.password).should == @user
- User.find_by_email(@user.email).try(:authenticate, @user.password).should == @user
- end
- it "should not find a user" do
- # Correct username, but wrong password
- User.find_by_username(@user.username).try(:authenticate, "something_else").should be(false)
- # Wrong username, but correct password
- User.find_by_username("jack_sparrow").try(:authenticate, @user.password).should be(nil)
- end
- end
- context "#create" do
- it "should create user" do
- User.create(username: "spiderman", password: "pass",
- password_confirmation: "pass", email: "mail@mail.com").should be_instance_of(User)
- end
- context "#should not create user" do
- before :all do
- @valid_user_attributes = {
- username: 'batman',
- password: 'pass',
- password_confirmation: 'pass',
- email: 'mail@mail.com'
- }
- end
- it "has problem with username" do
- # Empty username
- User.create(@valid_user_attributes.except(:username)).error_on(:username)
- .should eql(["can't be blank", "is too short (minimum is 3 characters)"])
- # Existing username
- User.create(@valid_user_attributes.update({username: @user.username})).error_on(:username)
- .should eql(["has already been taken"])
- end
- it "it has problem with password" do
- # Empty passwords
- User.create(@valid_user_attributes.except(:password, :password_confirmation)).error_on(:password)
- .should eql(["can't be blank"])
- # Not matching passwords
- User.create(@valid_user_attributes.update({password: 'wrong!'})).error_on(:password)
- .should eql(["doesn't match confirmation"])
- end
- it "has problem with email" do
- # Empty email
- User.create(@valid_user_attributes.except(:email)).error_on(:email)
- .should eql(["can't be blank", "is not an email"])
- User.create(@valid_user_attributes.update({email: "aint_email"})).error_on(:email)
- .should eql(["is not an email"])
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment