回调未保存价值

时间:2017-11-29 13:30:15

标签: ruby-on-rails rspec callback

我正在为一个相对复杂的动作编写一些测试,我需要在记录被销毁时发生。

这些是我的CheckIn函数和CheckIn has_one :weigh_in

def weight_loss
  prev_ci = CheckIn.joins(:weigh_in)
                   .where(client_id: self.client_id)
                   .where('week < ?', self.week)
                   .where('weigh_ins.current_weight IS NOT NULL')
                   .order('week desc').first()


  return 0 if self.weigh_in.nil? or prev_ci.nil? or prev_ci.weigh_in.nil?
  change = prev_ci.weigh_in.current_weight.to_f - self.weigh_in.current_weight.to_f
  return change.round(2)
end

def prev_ci
    prev_ci = CheckIn.joins(:weigh_in)
                   .where(client_id: self.client_id)
                   .where('week < ?', self.week)
                   .where('weigh_ins.current_weight IS NOT NULL')
                   .order('week desc').first()
  return prev_ci
end

def post_ci
    post_ci = CheckIn.joins(:weigh_in)
                               .where(client_id: self.client_id)
                               .where('week > ?', self.week)
                               .where('weigh_ins.current_weight IS NOT NULL')
                               .order('week desc').first()

    return nil if post_ci.nil? or post_ci.weigh_in.nil?
    return post_ci
end

在我的WeighIn模型中,我有以下回调:

class WeighIn < ActiveRecord::Base
  belongs_to :check_in

  before_save :add_weightloss
  after_destroy :adjust_weightloss

  def add_weightloss
    self.weightloss = 0
    self.weightloss = self.check_in.weight_loss
  end

  def adjust_weightloss
    post_ci = self.check_in.post_ci

    unless post_ci.nil?
        post_ci.weigh_in.weightloss = 0
        post_ci.weigh_in.weightloss = post_ci.weight_loss

        # Prints the correct value to pass the test (39.7)
        p 6, post_ci.weigh_in
        post_ci.weigh_in.save!
    end
  end
end

但我的最终测试(删除)仍然失败:

RSpec.describe WeighIn, type: :model do
  it "before_save weigh_in" do
      ci1 = CheckIn.create!(client_id: 1, program_id: 1, week: 1)
      ci2 = CheckIn.create!(client_id: 1, program_id: 1, week: 2)
      ci3 = CheckIn.create!(client_id: 1, program_id: 1, week: 3)
      ci4 = CheckIn.create!(client_id: 1, program_id: 1, week: 4)

      wi1 = WeighIn.create!(check_in_id: ci1.id, client_id: 1, date: Date.today, current_weight: 100)
      wi2 = WeighIn.create!(check_in_id: ci2.id, client_id: 1, date: Date.today, current_weight: 90)
      wi3 = WeighIn.create!(check_in_id: ci4.id, client_id: 1, date: Date.today, current_weight: 70.5)

      # Verifies functionality of before save
      expect(wi1.weightloss).to eq 0
      expect(wi2.weightloss).to eq 10
      expect(wi3.weightloss).to eq 19.5

      # Verifies fucntionality of update
      wi_params = { check_in_id: ci4.id, client_id: 1, date: Date.today, current_weight: 60.3 }
      wi3.update(wi_params)
      expect(wi3.weightloss).to eq 29.7

      # Verifies functionality of destroy
      wi2.destroy

      # Prints incorrect, old value, 29.7
      p 'in test', wi3
      expect(wi3.weightloss).to eq 39.7
    end
end

似乎虽然功能正常但记录还原或从未保存/更新?

1 个答案:

答案 0 :(得分:1)

您可以尝试重新加载对象以检查更改吗?

expect(wi3.reload.weightloss).to eq 39.7