I'm building some test for user account creation, and am running into an issue with one of my tests. The test is supposed to verify that the username cannot be used more than one time, and for some reason the test is failing, even though the behavior is working as expected in the actual application itself.
What's strange is I have a similar test set up to prevent duplicate email addresses, and that test is actually working as expected. Since they're set up very similarly, I'm surprised one is working but the other is not.
I'm posting the entire test file, as well as the model and migration files, below. The specific test that's failing is the fifth test, "test duplicate username." If there are any other files I can provide that may help in troubleshooting this issue.
RSpec Test File:
require 'rails_helper'
RSpec.describe User, type: :model do
# initiate new users
let(:user) {User.new}
let(:user1) {User.new}
# test new account without username
it "is not valid without a username" do
expect(user).not_to be_valid
end
# test username min length
it "must have at least 5 chars in username" do
user.username = 'a' * 4
expect(user).not_to be_valid
end
# test username max length
it "must have fewer than 25 chars in username" do
user.username = 'a' * 26
expect(user).not_to be_valid
end
# test proper username length
it "must have between 5-25 chars in username" do
user.username = 'a' * 6
expect(user).to be_valid
end
# test duplicate usernames
it "cannot use the same username multiple times" do
user.username = 'aBCDEF'
user1.username = 'aBCDEF'
expect(user1).not_to be_valid
end
# test password min length
it "must have at least 6 chars in password" do
user.password = 'a' * 5
expect(user).not_to be_valid
end
#test password max length
it "must have no more than 25 chars in password" do
user.password = 'a' * 26
expect(user).not_to be_valid
end
# test multiple email addresses
it "cannot use the same email multiple times" do
user.email = '[email protected]'
user1.email = '[email protected]'
expect(user1).not_to be_valid
end
end
User Model:
class User < ApplicationRecord
USERNAME_LENGTH = (5..25)
PASSWORD_LENGTH = (6..25)
validates_presence_of :username
validates :username, length: USERNAME_LENGTH, uniqueness: true
validates :password, length: PASSWORD_LENGTH, allow_nil: true
validates :email, uniqueness: true
has_many :posts
has_one :cart
attr_reader :password
def self.find_from_credentials(username, password)
user = find_by(username: username)
return nil unless user
user if user.is_password?(password)
end
def is_password?(password_attempt)
BCrypt::Password.new(password_digest).is_password?(password_attempt)
end
def password=(password)
@password = password
self.password_digest = BCrypt::Password.create(password)
end
end
User Migration File:
class CreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :username
t.string :session_token
t.string :password_digest
t.string :name
t.string :email
t.boolean :admin
t.timestamps
end
add_index :users, :username
add_index :users, :session_token
end
end