|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe User do |
| 4 | + |
| 5 | + before do |
| 6 | + @user = User.new(name: "Example User", email: "[email protected]", |
| 7 | + password: "foobar", password_confirmation: "foobar") |
| 8 | + end |
| 9 | + |
| 10 | + subject { @user } |
| 11 | + |
| 12 | + it { should respond_to(:name) } |
| 13 | + it { should respond_to(:email) } |
| 14 | + it { should respond_to(:password_digest) } |
| 15 | + it { should respond_to(:password) } |
| 16 | + it { should respond_to(:password_confirmation) } |
| 17 | + it { should respond_to(:authenticate) } |
| 18 | + |
| 19 | + |
| 20 | + it { should be_valid } |
| 21 | + describe "when name is not present" do |
| 22 | + before { @user.name = " " } |
| 23 | + it { should_not be_valid } |
| 24 | + end |
| 25 | + |
| 26 | + describe "when email is not present" do |
| 27 | + before { @user.email = " " } |
| 28 | + it { should_not be_valid } |
| 29 | + end |
| 30 | + |
| 31 | + describe "when name is too long" do |
| 32 | + before { @user.name = "a" * 51 } |
| 33 | + it { should_not be_valid } |
| 34 | + end |
| 35 | + |
| 36 | + describe "when email format is invalid" do |
| 37 | + it "should be invalid" do |
| 38 | + addresses = %w[user@foo,com user_at_foo.org example.user@foo. |
| 39 | + foo@bar_baz.com foo@bar+baz.com] |
| 40 | + addresses.each do |invalid_address| |
| 41 | + @user.email = invalid_address |
| 42 | + expect(@user).not_to be_valid |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe "when email format is valid" do |
| 48 | + it "should be valid" do |
| 49 | + |
| 50 | + addresses.each do |valid_address| |
| 51 | + @user.email = valid_address |
| 52 | + expect(@user).to be_valid |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + describe "when email address is already taken" do |
| 58 | + before do |
| 59 | + user_with_same_email = @user.dup |
| 60 | + user_with_same_email.email = @user.email.upcase |
| 61 | + user_with_same_email.save |
| 62 | + end |
| 63 | + |
| 64 | + it { should_not be_valid } |
| 65 | + end |
| 66 | + |
| 67 | + describe "when password is not present" do |
| 68 | + before do |
| 69 | + @user = User.new(name: "Example User", email: "[email protected]", |
| 70 | + password: " ", password_confirmation: " ") |
| 71 | + end |
| 72 | + it { should_not be_valid } |
| 73 | + end |
| 74 | + |
| 75 | + describe "when password doesn't match confirmation" do |
| 76 | + before { @user.password_confirmation = "mismatch" } |
| 77 | + it { should_not be_valid } |
| 78 | + end |
| 79 | + |
| 80 | + describe "with a password that's too short" do |
| 81 | + before { @user.password = @user.password_confirmation = "a" * 5 } |
| 82 | + it { should be_invalid } |
| 83 | + end |
| 84 | + |
| 85 | + describe "return value of authenticate method" do |
| 86 | + before { @user.save } |
| 87 | + let(:found_user) { User.find_by(email: @user.email) } |
| 88 | + |
| 89 | + describe "with valid password" do |
| 90 | + it { should eq found_user.authenticate(@user.password) } |
| 91 | + end |
| 92 | + |
| 93 | + describe "with invalid password" do |
| 94 | + let(:user_for_invalid_password) { found_user.authenticate("invalid") } |
| 95 | + |
| 96 | + it { should_not eq user_for_invalid_password } |
| 97 | + specify { expect(user_for_invalid_password).to be_false } |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + |
| 102 | +end |
0 commit comments