【问题标题】:Model's validation test does not work in rspec模型的验证测试在 rspec 中不起作用
【发布时间】:2021-02-27 13:51:48
【问题描述】:

我想看看验证是否正常工作,所以我在 Rspec 中编写了一个模型测试。

models/work_history.rb

class WorkHistory < ApplicationRecord
  belongs_to :account

  validates :name,
            :since_date,
            :position, presence: true
  validates :is_employed, inclusion: [true, false]
  validates :until_date, presence: true, if: -> { :is_employed == false }
end

工厂/work_histories.rb

FactoryBot.define do
  factory :work_history do
    sequence(:name) { |n| "#{n}_company" }
    since_date { '2017-04-01' }
    until_date { '2021-03-01' }
    is_employed { false }
    position { 'director' }
    department { 'sales' }

    association :account
  end
end

spec/models/work_history_spec.rb

RSpec.describe WorkHistory, type: :model do
  let(:account) { create(:account) }
  let(:work_history) { build(:work_history, account: account) }

  it "is invalid when is_employed is false and until_date is nil" do
    work_history.is_employed = false
    work_history.until_date = ''
    expect(work_history.valid?).to eq(false)
  end

  it "is invalid when is_employed is employed" do
    work_history.is_employed = 'employed'
    expect(work_history.valid?).to eq(false)
  end
end

然后我运行 rspec 命令,但测试没有通过。

这是 rspec 测试错误日志

docker-compose run app rspec spec/models
Creating toei-works_app_run ... done
FF

Failures:

  1) WorkHistory is invalid when is_employed is false and until_date is nil
     Failure/Error: expect(work_history.valid?).to eq(false)

       expected: false
            got: true

       (compared using ==)

       Diff:
       @@ -1 +1 @@
       -false
       +true

     # ./spec/models/work_history_spec.rb:10:in `block (2 levels) in <top (required)>'

  2) WorkHistory is invalid when is_employed is employed
     Failure/Error: expect(work_history.valid?).to eq(false)

       expected: false
            got: true

       (compared using ==)

       Diff:
       @@ -1 +1 @@
       -false
       +true

     # ./spec/models/work_history_spec.rb:15:in `block (2 levels) in <top (required)>'

我不知道为什么 Rspec 测试失败

为什么我不能通过 rspec 的测试?

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

    您没有正确构建验证的 if 部分。您当前正在检查符号 :is_employed? 是否等于 false,它永远不会。您可以在此处查看正确的语法:

    https://guides.rubyonrails.org/active_record_validations.html#conditional-validation

    你可以这样做

    class WorkHistory < ApplicationRecord
      belongs_to :account
    
      validates :name,
                :since_date,
                :position, presence: true
      validates :is_employed, inclusion: [true, false]
      validates :until_date, presence: true, if: -> { :not_employed? }
    
      private
    
      def not_enployed?
        !is_employed?
      end
    end
    

    class WorkHistory < ApplicationRecord
      belongs_to :account
    
      validates :name,
                :since_date,
                :position, presence: true
      validates :is_employed, inclusion: [true, false]
      validates :until_date, presence: true, unless: Proc.new { |work_history| work_history.is_employed? }
    end
    

    【讨论】:

    • 感谢您的回答。我按照我的指示实现了它并且它工作正常!!
    猜你喜欢
    • 1970-01-01
    • 2018-10-08
    • 2011-11-24
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    相关资源
    最近更新 更多