簡體   English   中英

如何使用rspec測試我的設計代碼?

[英]How to test my devise code using rspec?

這是我設計的單元測試代碼。 有什么方法可以編寫更好的測試代碼?

user_spec.rb:

require 'rails_helper'

    RSpec.describe User, type: :model do

      it { should have_many(:companies) }
      it { should have_many(:jobs) }
      it do
               should validate_length_of(:encrypted_password).
                   is_at_least(6).
                   on(:create)
               end

      it { is_expected.to validate_presence_of(:email) }
      it { is_expected.to validate_presence_of(:encrypted_password) }
      it { should have_db_column(:id) }
      it { should have_db_column(:email) }
      it { should have_db_column(:encrypted_password) }
      it { should have_db_column(:confirmation_token) }
      it { should have_db_column(:confirmed_at) }
      it { should have_db_column(:confirmation_sent_at) }

      it 'is databse authenticable' do
        user = User.create(
           email: 'test@example.com', 
          password: 'password123',
          password_confirmation: 'password123'
        )
        expect(user.valid_password?('password123')).to be_truthy
      end
end

好的-據我了解,您正在詢問測試Rails模型的最佳實踐。 它是由Devise自動生成的,這一事實在很大程度上是無關緊要的。

我認為這里的第一件事情就是這樣 ,你實際測試-大部分代碼這里測試設計的底層實現。 那不應該由您的應用程序的測試套件負責–庫中有很多測試可以為您進行測試。

唯一真正有用的測試是IMO,它是關聯測試(看起來像來自應當shoulda-matchers )。 其余的斷言是測試特定於Devise的代碼,該代碼將您的測試套件與第三方庫緊密結合在一起–但這只會使您陷入痛苦之中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM