为什么在我的 RSPEC 测试中没有在对象上调用此 before_save?

Why is this before_save not being called on an object in my RSPEC test?

我有以下 PositionGroup 型号:

# == Schema Information
#
# Table name: position_groups
#
#  id                     :bigint           not null, primary key
#  ticker                 :string
#  total_spend            :float
#  total_units            :integer
#  total_value            :float
#  created_at             :datetime         not null
#  updated_at             :datetime         not null
#  portfolio_id           :integer
#
class PositionGroup < ApplicationRecord
  has_many :positions
  belongs_to :portfolio

  before_save :update_total_units

  def update_total_units
    self.positions.each do |p|
      self.total_units = p.volume + self.total_units
    end
  end
end

在我的 position_group_spec.rb 中,我有以下内容:

RSpec.describe PositionGroup, type: :model do
  price1 = 10.00
  price2 = 15.00
  let(:stock) { create(:stock, price: price1) }
  let(:portfolio) { create(:portfolio) }
  let(:pg) { create(:position_group, portfolio: portfolio) }
  let(:position1) { create(:position, stock: stock, portfolio: portfolio, position_group: pg)}
  let(:position2) { create(:position, stock: stock, portfolio: portfolio, position_group: pg)}

  before do
    stock
    portfolio
    pg
    position1
    position2
    pg.positions << [position1, position2]
  end

  context "associations" do
    it { should belong_to(:portfolio) }
  end

  context "methods" do

    it "should calculate the total # of units" do
      # (Position 1 Volume + Position 2 Volume)
      # 100 + 100 = 200
      binding.pry
      expect(pg.total_units).to eql 200
    end

然而,当我 运行 它在第一个测试中达到 binding.pry 时,它告诉我 pg.total_units 没有设置。

    49:     it "should calculate the total # of units" do
    50:       # (Position 1 Volume + Position 2 Volume)
    51:       # 100 + 100 = 200
 => 52:       binding.pry
    53:       expect(pg.total_units).to eql 200
    54:     end
    55: 

[1] pry(#<RSpec::ExampleGroups::PositionGroup::Methods>)> pg
=> #<PositionGroup:0x00007fd63d4e41c8
 id: 2,
 ticker: nil,
 portfolio_id: 4,
 created_at: Tue, 17 Mar 2020 08:05:42 UTC +00:00,
 updated_at: Tue, 17 Mar 2020 08:05:42 UTC +00:00,
 total_units: nil,
 total_spend: nil,
 total_value: nil,
[2] pry(#<RSpec::ExampleGroups::PositionGroup::Methods>)> pg.positions.count
=> 2
[4] pry(#<RSpec::ExampleGroups::PositionGroup::Methods>)> pg.total_units
=> nil

那么为什么这个 let 语句没有在我的 PositionGroup 模型中触发那个 before_save 回调?

let(:position1) { create(:position, stock: stock, portfolio: portfolio, position_group: pg)}

Rails has_many 添加不会在模型上调用 before_save 回调。

您可以在关联上使用 before_add 回调。例如,像这样:

class PositionGroup < ApplicationRecord
  has_many :positions, before_add: :update_total_units
  belongs_to :portfolio

  def update_total_units(position)
    update(total_units: total_units + position.volume)
  end
end

文档在这里:https://guides.rubyonrails.org/association_basics.html#association-callbacks