1

我正在学习为我的代码编写规范,但我还是新手。尝试为我的模型问题编写规范/共享示例,但我不明白如何编写它,因为我发现这非常复杂。

如果有人可以帮助我或向我展示如何为这些代码编写规范,那将非常有帮助。下面是我关心的 AddStakeholder。

module AddStakeholder
  extend ActiveSupport::Concern

  included do
    before_validation -> { add_stakeholders }
  end

  private

  def add_stakeholders
    return unless self.stakeholder

    company = if self.is_a?(Certificate)
                self.round ? self.round.company : self.company
              else
                self.company
              end
    existing_stakeholder = company.stakeholders.find_by_email(self.stakeholder.email)
    if existing_stakeholder.present?
      if self.stakeholder.name == existing_stakeholder.name
        self.stakeholder = existing_stakeholder
      else
        self.stakeholder.company = company
      end
    else
      self.stakeholder.company = company
    end
  end
end
4

1 回答 1

0

您的代码片段中没有足够的信息来准备一个完整的示例,但它看起来有点类似于:

RSpec.describe Thing, type: :model do
  it "adds stakeholders" do
    t = Thing.new(...)
    expect(t).to be_valid # or .to_not be_valid depending on your validations on Thing
    expect(t.stakeholder).to ... # whatever you expect it to be
  end
end
于 2019-06-27T21:31:58.707 回答