모델의 before_update 콜백을 테스트하려고합니다.Rspec 및 FactoryGirl을 사용하여 레일스 모델에서 before_update 콜백을 테스트하는 방법은 무엇입니까?
모델/option.rb :
class Option < ApplicationRecord
belongs_to :activity
has_many :suboptions, class_name: "Option", foreign_key: "option_id"
belongs_to :parent, class_name: "Option", optional: true, foreign_key: "option_id"
accepts_nested_attributes_for :suboptions, allow_destroy: true,
reject_if: ->(attrs) { attrs['name'].blank? }
validates :name, presence: true
before_create :set_defaults
before_update :set_updates
def set_defaults
self.suboptions.each do |sbp|
sbp.activity_id = self.activity_id
end
end
def set_updates
suboptions.each do |child|
child.activity_id = self.activity_id
end
end
end
사양/모델/option.rb :
require 'rails_helper'
RSpec.describe Option, type: :model do
describe "Callbacks" do
it "before_create" do
suboption = create(:option)
option = create(:option, suboptions:[suboption])
option.run_callbacks(:create) {false}
expect(option.suboptions.first.activity_id).to eq suboption.activity_id
end
it "before_update" do
end
end
end
내가 성공적으로 before_create 콜백을 테스트 (적어도 그것이 나에게 올바른 결과를 주었다). 하지만 before_update 콜백을 테스트하는 방법을 모르겠습니다. 그것을 할 수있는 방법이 있습니까?