2017-08-24 345 views
0

我是紅寶石軌道上的新手。我試圖將一些代碼分解成一個方法,並調用該方法,因爲我想重用該算法。問題是,當我這樣做時,覆蓋該方法的單元測試失敗。爲什麼我的單元測試失敗時,我重構

商業邏輯是

begin 
     generate_identifier_ladok_proof 
     puts Ladok.api.save_education_event(self, current_user) 
    rescue => e 
     logger.warn { "Approving EducationEvent(ID: #{id}) failed: # 
     {e.message}\n\t#{e.backtrace.join("\n\t")}" } 
     errors.add(:base, e.message) 
     return false 
    end 

    def generate_identifier_ladok_proof 
     # Generate a new identifier if event has no identifier 
     generate_identifier if identifier.nil? 

     # Change identifier if identifier already exists in Ladok 
     # (assuming we only send education events to Ladok once and once only) 
     count_no_of_attempts = 0 
     while (Ladok.api.education_event_exists?(self) && count_no_of_attempts < 5) 
     generate_identifier 
     count_no_of_attempts += 1 
     end 
     if (Ladok.api.education_event_exists?(self)) 
     errors.add(:base,"Utbildningstillfället #{identifier} med ID #{id} finns redan i LADOK. 
        Fem försök att byta till ett ID som inte finns har misslyckats. Kontakta Studentavdelningen eller försök igen.") 
     return false 
     end 
    end 

和失敗的測試是

def test_click_event_change_status_ready_for_review_should_fail_and_produce_warning 
     event = education_events(:course_event_status_ready_for_review) 
     post :save_education_event, :change_status => 'xyz', :event_id => event.id, :education_event => event.attributes 
     assert_response :redirect 
     assert_redirected_to :action => 'edit', :id => event.education.id 
ee_in_db = EducationEvent.find(event.id) 
     assert_not_nil flash[:warning], 'There are supposed to be warnings here!' 
     assert_nil flash[:notice], 'Notice were found but not expected.' 
     assert_not_nil ee_in_db.identifier, 'EducationEvent identifier is not supposed to be nil.' 
     assert_equal EventStatus::READY_FOR_REVIEW, ee_in_db.status, "EducationEvent status is supposed to be READY_FOR_REVIEW, not #{ee_in_db.status}." 
    end 

上排

assert_nil flash[:notice], 'Notice were found but not expected.' 

混亂的部分是,當我跳過重構和有業務邏輯看起來如下

begin 
     # Generate a new identifier if event has no identifier 
     generate_identifier if identifier.nil? 

     # Change identifier if identifier already exists in Ladok 
     # (assuming we only send education events to Ladok once and once only) 
     count_no_of_attempts = 0 
     while (Ladok.api.education_event_exists?(self) && count_no_of_attempts < 5) 
     generate_identifier 
     count_no_of_attempts += 1 
     end 
     if (Ladok.api.education_event_exists?(self)) 
     errors.add(:base,"Utbildningstillfället #{identifier} med ID #{id} finns redan i LADOK. 
        Fem försök att byta till ett ID som inte finns har misslyckats. Kontakta Studentavdelningen eller försök igen.") 
     return false 
     end 
     puts Ladok.api.save_education_event(self, current_user) 
    rescue => e 
     logger.warn { "Approving EducationEvent(ID: #{id}) failed: # 
     {e.message}\n\t#{e.backtrace.join("\n\t")}" } 
     errors.add(:base, e.message) 
     return false 
    end 

然後測試通過。我懷疑這裏有一些範圍問題,我不明白。

回答

2

你沒有處理重構generate_identifier_ladok_proof的回報。

在你原來的代碼,你有:

if (Ladok.api.education_event_exists?(self)) 
    errors.add(:base,"Utbildningstillfället #{identifier} med ID #{id} finns redan i LADOK. 
       Fem försök att byta till ett ID som inte finns har misslyckats. Kontakta Studentavdelningen eller försök igen.") 
    return false 
    end 
    puts Ladok.api.save_education_event(self, current_user) 

所以,如果Ladok.api.education_event_exists?(self)是真的,你絕然save_education_event

但新代碼,return false裏面generate_identifier_ladok_proof,但你從來沒有檢查過返回值。

begin 
    generate_identifier_ladok_proof 
    puts Ladok.api.save_education_event(self, current_user) 
rescue => e 

所以你應該讓generate_identifier_ladok_proof返回true或false,並檢查它。

begin 
    if !generate_identifier_ladok_proof 
    return false 
    end 
    puts Ladok.api.save_education_event(self, current_user) 
rescue => e 
    logger.warn { "Approving EducationEvent(ID: #{id}) failed: # 
    {e.message}\n\t#{e.backtrace.join("\n\t")}" } 
    errors.add(:base, e.message) 
    return false 
end 

def generate_identifier_ladok_proof 
    # Generate a new identifier if event has no identifier 
    generate_identifier if identifier.nil? 

    # Change identifier if identifier already exists in Ladok 
    # (assuming we only send education events to Ladok once and once only) 
    count_no_of_attempts = 0 
    while (Ladok.api.education_event_exists?(self) && count_no_of_attempts < 5) 
    generate_identifier 
    count_no_of_attempts += 1 
    end 
    if (Ladok.api.education_event_exists?(self)) 
    errors.add(:base,"Utbildningstillfället #{identifier} med ID #{id} finns redan i LADOK. 
       Fem försök att byta till ett ID som inte finns har misslyckats. Kontakta Studentavdelningen eller försök igen.") 
    return false # fail 
    else 
    return true # success 
    end 
end 
相關問題