2017-11-29 3 views
1

레코드가 삭제 될 때 발생해야하는 비교적 복잡한 작업에 대한 몇 가지 테스트를 작성하고 있습니다.콜백 값 저장 안 함

이 내 CheckIn 기능이며이 CheckInhas_one :weigh_in :

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 

나는 다음과 같은 콜백 있습니다

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