2017-11-29 21 views
1

レコードが破棄されたときに発生する必要がある、比較的複雑な操作のテストをいくつか書いています。私は、次のコールバックを持っている私のWeighInモデルでコールバックで値が保存されない

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 

これらは私のCheckIn機能とCheckInhas_one :weigh_inある

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

これを試すことができますか? 'オブジェクトをリロードするために(wi3.reload.weightloss).to eq 39.7を期待する – Nathan

+0

w3をリロードしようとしましたか? – Manishh

+0

ああ、やった!ありがとうございました。私は何かが足りないと思った。 –

答えて

1

オブジェクトをリロードして変更を確認することができますか?

expect(wi3.reload.weightloss).to eq 39.7 
関連する問題