繁体   English   中英

使用不带辅助工具的rspec在Rails模型中测试字段唯一性的验证

[英]Testing the validation of uniqueness of the field in Rails's model using rspec without helper gems

我正在尝试编写测试以验证RoR模型中字段的唯一性。

我正在研究Ruby on Rails应用程序,该应用程序用于在TDD中练习我的技能。 到目前为止,我已经使用Internet作为我的资源。 我正在编写关于模型验证的测试,我不会使用'shoulda','FactoryGirl'(等等)gems。 我知道使用这些gems可以节省很多代码,但最终我会使用这些gems。 我想学习如何自己编写没有rem的rspec测试,以帮助我理解如何编写测试。 到目前为止,在“唯一性”测试之前,我一直做得很好。

我如何创建测试以验证“用户”模型中“电子邮件”字段的唯一性,而不使用“ shoulda”,“ FactoryGirl”(等)gem。 我知道使用这些gems可以节省很多代码,但最终我会使用这些gems。 我想学习如何自己编写没有rem的rspec测试,以帮助我理解如何编写测试。

在Stackoverflow(以及网络上的其他地方)上,对该问题的大多数“答案”都包括使用这些辅助工具。 但是,如果没有宝石,找不到答案。

这是模型User.rb

class User < ApplicationRecord
  validate: :name, :email, presence: true
  validates :name, length: { minimum: 2 }
  validates :name, length: { maximum: 50 }
  # validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create }
  validates :email, uniqueness: true
end

And here is `user_spec.rb`.

require 'rails_helper'

RSpec.describe User, type: :model do
  subject {
    described_class.new(name: 'John', email: 'john@home.xyz')
  }

  describe 'Validation' do
    it 'is valid with valid attributes' do
      expect(subject).to be_valid
    end

    it 'is not valid without name' do
      subject.name = nil
      expect(subject).to_not be_valid
    end

    it 'is not valid without email' do
      subject.email = nil
      expect(subject).to_not be_valid
    end

    (...)

    it 'is invalid if the email is not unique' do
      expect(????????).to_not be_valid
    end

  end
end

```

如何写以测试唯一性。 除了“主题”以外,我还应该使用其他东西进行测试吗? 请记住,这次我不需要使用宝石的解决方案(Shoulda / FactoryGirl / etc)。

最近几天我一直在这里,没有运气。 有什么办法吗? 在Ruby on Rails上关于rspec的最佳教程在哪里?

要测试唯一性,首先必须创建一个用户,该用户的电子邮件与subject使用的电子邮件相同。 然后,由于电子邮件上的唯一性验证,您的subject将无效。

就像是:

before { described_class.create!(name: 'foo', email: 'john@home.xyz') }
it 'is invalid if the email is not unique' do
  expect(subject).to be_invalid
end

好吧,我知道了。 好的,在@Jagdeep Singh的建议下,我编写了此测试:

```

context 'when email is not unique' do
  before { described_class.create!(name: 'foo', email: 'john@home.xyz') }
  it {expect(subject).to be_invalid}
end

context 'when email is unique' do
  before { described_class.create!(name: 'foo', email: 'jane@home.xyz') }
  it {expect(subject).to be_valid}
end

似乎通过了测试。 我添加了其他测试来测试有效的唯一性。 谢谢您的帮助。

我决定重写整个测试,以利用context使其更清晰易读。 这是我的重写:

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe User, type: :model do
  subject {
    described_class.new(name: 'John', email: 'john@home.xyz')
  }

  describe '.validation' do



    context 'when email is not unique' do
      before { described_class.create!(name: 'foo', email: 'john@home.xyz') }
      it {expect(subject).to be_invalid}
    end

    context 'when email is  unique' do
      before { described_class.create!(name: 'foo', email: 'jane@home.xyz') }
      it {expect(subject).to be_valid}
    end

  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM