簡體   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