属于Spec在RSpec中失败

时间:2019-01-10 17:10:25

标签: ruby-on-rails ruby rspec-rails shoulda

我有一个称为Option的模型。

class Option < ApplicationRecord
  belongs_to :user
  belongs_to :company
  belongs_to :scheme
  validate :check_for_quantity

  def check_for_quantity
    if self.quantity > self.scheme.remaining_options
      errors.add(:quantity, "cannot be more than the remaining options #{ self.scheme.remaining_options.to_i}")
    end
  end
end

和一个称为Scheme的模型。

class Scheme < ApplicationRecord
  belongs_to :share_class
  belongs_to :equity_pool
  belongs_to :company
  has_many :options, dependent: :destroy

  attr_accessor :percentage

  def ownership
    self.remaining_options * 100 / self.company.total_fdsc
  end

  def remaining_options
    self.initial_size - self.options.sum(&:quantity)
  end
end

我对期权模型的说明如下

require 'rails_helper'

RSpec.describe Option, type: :model do

  describe "Associations" do
    subject { create (:option) }
    it { is_expected.to belong_to(:scheme) } 
    it { is_expected.to belong_to(:vesting_schedule).optional }
    it { is_expected.to belong_to(:user) }
    it { is_expected.to belong_to(:company) }
  end
end

当我运行此规范时,第一个示例给出了错误

1)期权协会应属于要求的方案:true

 Failure/Error: if self.quantity > self.scheme.remaining_options

 NoMethodError:
   undefined method `remaining_options' for nil:NilClass
 # ./app/models/option.rb:9:in `check_for_quantity'

这是什么问题?

我的选项工厂机器人

FactoryBot.define do
  factory :option do
    security "MyString"
    board_status false
    board_approval_date "2018-08-16"
    grant_date "2018-08-16"
    expiration_date "2018-08-16"
    quantity 1
    exercise_price 1.5
    vesting_start_date "2018-08-16"
    vesting_schedule nil
    scheme
    user
    company
  end
end

1 个答案:

答案 0 :(得分:1)

只需在验证中添加一个条件,以便在关联为nil时不触发该条件。

class Option < ApplicationRecord
  belongs_to :user
  belongs_to :company
  belongs_to :scheme
  validate :check_for_quantity, unless: -> { self.scheme.nil? }

  def check_for_quantity
    if self.quantity > self.scheme.remaining_options
      errors.add(:quantity, "cannot be more than the remaining options #{ self.scheme.remaining_options.to_i}")
    end
  end
end

您可能还想确保self.quantity是一个数字,而不是nil,以避免NoMethodError: undefined method > for nil:NilClass可以通过数字验证来完成。

class Option < ApplicationRecord
  belongs_to :user
  belongs_to :company
  belongs_to :scheme
  validates_numericality_of :quantity
  validate :check_for_quantity, if: -> { self.scheme && self.quantity }

  def check_for_quantity
    if self.quantity > self.scheme.remaining_options
      errors.add(:quantity, "cannot be more than the remaining options #{ self.scheme.remaining_options.to_i}")
    end
  end
end