ruby-on-rails - 使用重复条目测试 Rails 模型的唯一性

标签 ruby-on-rails ruby rspec rspec-rails

我对我的 Rails 数据库有一个强制使用唯一用户名的限制,并且我在每个模型测试开始时创建一个用户。我正在尝试使用与第一个用户名相同的用户名创建第二个用户,我希望这无效,但它返回时有效。

我尝试调整代码以在生成新用户时使用 new、save 和 create 方法,但没有成功。

注册模型:

class Registration < ApplicationRecord
  @username_length = (3..20)
  @password_requirements = /\A
    (?=.{8,}) # Password must be at least 8 characters
    (?=.*\d) # Password must contain at least one number
    (?=.*[a-z]) # Password must contain at least one lowercase letter
    (?=.*[A-Z]) # Password must contain at least one capital letter
    # Password must have special character
    (?=.*[['!', '@', '#', '$', '%', '^', '&']])
  /x

  validates :username, length: @username_length, uniqueness: true
  validates :password, format: @password_requirements
  validates :email, uniqueness: true
  has_secure_password
  has_secure_token :auth_token

  def invalidate_token
    self.update_columns(auth_token: nil)
  end

  def self.validate_login(username, password)
    user = Registration.find_by(username: username)
    if user && user.authenticate(password)
      user
    end
  end
end

注册测试:

require 'rails_helper'

RSpec.describe Registration, type: :model do
  before do
    @user = Registration.new(
      username: '1234',
      password: 'abcdeF7#',
      email: 'test@test.com',
      name: 'One'
    )
  end
  it 'should not be valid if the username is already taken' do
    @user.save!(username: '1234')
    expect(@user).not_to be_valid
  end
end

我希望这个测试能够通过,因为它是一个重复的用户名。

最佳答案

正如 fabio 所说,您没有第二个注册对象来检查唯一性。 您刚刚检查了您保存的@user 是否有效,它始终有效并保存在数据库中。要检查您的唯一性验证,您可以这样做 -

RSpec.describe Registration, type: :model do
  before do
    @user = Registration.create(
      username: '1234',
      password: 'abcdeF7#',
      email: 'test@test.com',
      name: 'One'
    )
    @invalid_user = @user.dup
    @invalid_user.email = "test1@test.com"
  end
  it 'should not be valid if the username is already taken' do
    expect(@invalid_user.valid?).should be_falsey
  end
end

关于ruby-on-rails - 使用重复条目测试 Rails 模型的唯一性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57156309/

相关文章:

ruby-on-rails - 如何使用 postgres 和 ActiveRecord 检索所有重复的电子邮件

ruby-on-rails - 以线程安全的方式设置 Rails ActiveResource header

arrays - Ruby - 基于另一个数组过滤哈希数组

ruby-on-rails - 如何使用 rspec 在 guard-spork 中重新加载 Controller 缓存类

rspec - 如何配置 Aptana 3 IDE 来为我的 Rails 项目运行 rspec 测试?

ruby-on-rails - 使用 Rails 3 的自定义守护进程

ruby - 如何使用 yardoc 列出未记录的模块/类/常量/方法?

ruby-on-rails - 从 Rails Helper 返回多个标签的最佳方式是什么?

ruby-on-rails - rspec .reload 不重新加载对象

ruby-on-rails - rails 3 教程 : rspec + factory_girl_rails problem