2012-08-01 84 views
0

您好我是新來的saleforce apex codiing,我已經編寫了一個觸發器,用於將轉換定製對象轉換爲賬戶和聯繫人。 。我已經寫了扳機,它的測試代碼,但我停留在35%的代碼覆蓋率u能請指出我要去的地方錯了無論我做什麼,salesforce的觸發代碼覆蓋率都保持在35%

這裏是我寫的觸發

trigger TransferDeals on Lead (after update) { 
Map<Id, Lead> leadMap = new Map<Id,Lead>(); 
Lead parent; 

for (Integer i = 0; i < Trigger.new.size(); i++){ 
if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false) { 
    leadMap.put(Trigger.new[i].Id, Trigger.new[i]); 
} 
} 

if(leadMap.size() > 0) { 
    Set<Id> leadIds = leadMap.keySet(); 
    List<Deal_Offer__c> allChildren = 
    [select Id, Account__c, Contact__c, Lead__c from Deal_Offer__c where lead__c in :leadIds]; 

System.debug(allChildren); 

    for (Deal_Offer__c child : allChildren) { 
    if (leadMap.containsKey(child.Lead__c)) { 
     // lookup the parent lead 
     parent = leadMap.get(child.Lead__c); 
     // update the fields on the child object 
     child.account__c = parent.ConvertedAccountId; 
     child.Contact__c = parent.ConvertedContactId; 
    } 
    } 

System.debug(allChildren); 
//try { 
update allChildren; 
// } catch(Exception e) { 
     // could put something here to notify on error 
    // otherwise it fails silently 
// } 

} }

和這裏是我writen來驗證代碼

@isTest 
private class Leadtriggertest { 
static testMethod void verifyAccount(){ 
    // Perform our data preparation. 
    List<Lead> leads = new List<Lead>{}; 
    List<Deal_Offer__c> deals = new List<Deal_Offer__c>{}; 
    for(Integer i = 0; i < 200; i++){ 
     Lead a = new Lead(LastName = 'TestLead ' + i,Company = 'TesCompany' + i,IsConverted=false); 
     Deal_Offer__c b = new Deal_Offer__c(Name = 'TestDeal' + i,Lead__c = a.Id); 
     leads.add(a); 
     deals.add(b); 
    } 
    insert leads; 
    insert deals; 

    Map<Id, Lead> leadMap = new Map<Id,Lead>(); 
    Set<Id> leadIds = leadMap.keySet(); 
    List<Lead> allChildren = [select Id,LastName,IsConverted from Lead where Id in :leadIds]; 
     for (Lead child : allChildren) { 
      if(child.IsConverted==false){ 
       child.Isconverted = true; 
      } 
     } 

    // Start the test, this changes governor limit context to 
    // that of trigger rather than test. 
    test.startTest(); 

    // Insert the Account records that cause the trigger to execute. 
    update Leads; 

    // Stop the test, this changes limit context back to test from trigger. 
    test.stopTest(); 

    List<Deal_Offer__c> testdealsupdate = 
           [select Id, Account__c, Contact__c, Lead__c from Deal_Offer__c where lead__c in :leadIds]; 
    List<echosign_dev1__SIGN_Agreement__c> testagreementupdate = 
          [select Id, echosign_dev1__Account__c, echosign_dev1__Recipient_Lead__c from echosign_dev1__SIGN_Agreement__c where echosign_dev1__Recipient_Lead__c in :leadIds]; 
    List<Lead> leadcheckout = 
          [select Id,LastName,ConvertedAccountId,ConvertedContactId from Lead where Id in :leadIds]; 
    Map<Id,Lead> leadmap2= new Map<Id,Lead>(); 

    for(Lead c: leadcheckout){ 
     leadmap2.put(c.Id, c); 
    } 
    for(Deal_offer__c a : testdealsupdate){ 
     Lead b = leadMap2.get(a.Lead__c); 
     System.assertEquals(a.Account__c,b.ConvertedAccountId); 
     System.assertEquals(a.Contact__c,b.ConvertedContactId); 
    } 
    for(echosign_dev1__SIGN_Agreement__c a : testagreementupdate){ 
     Lead b = leadMap2.get(a.echosign_dev1__Recipient_Lead__c); 
     System.assertEquals(a.echosign_dev1__Account__c ,b.ConvertedAccountId); 
    } 

} 

}

測試類

請幫我找出如何提高代碼覆蓋率預先感謝您

+0

不要編寫代碼來改進代碼覆蓋率;編寫能夠提供價值的測試,例如,您希望代碼正確/正確地執行什麼操作?寫這些測試,然後看看你的覆蓋面如何改善。任何未涵蓋的代碼都會問自己「它是做什麼的?」,「它有什麼用途?」並編寫符合目的而不是執行的測試。 – 2012-08-01 22:45:36

+0

我同意@Shaun Wilde。嘗試並列出將在觸發器中執行不同分支的不同場景。 – Anup 2012-08-02 06:59:59

+0

將definelty嘗試這種方法..對快速回復...太快... – user1568642 2012-08-02 07:37:22

回答

0

我相信這是因爲沒有被執行的代碼,這部分(或者說,如果條件在那裏)。

for (Deal_Offer__c child : allChildren) { 
if (leadMap.containsKey(child.Lead__c)) { 
    // lookup the parent lead 
    parent = leadMap.get(child.Lead__c); 
    // update the fields on the child object 
    child.account__c = parent.ConvertedAccountId; 
    child.Contact__c = parent.ConvertedContactId; 
} 

你能通過在if語句中添加一個system.debug語句來驗證嗎? 此外,請嘗試打印出地圖中的所有潛在客戶ID,以及您在leadMap.containsKey (child.Lead__c)行中使用的相應child.Lead__c值。

Anup

+0

anup,Shaun ...觸發器在沙箱中完美工作...我必須測試*在測試類中的轉換操作*的領導和我想我正在寫錯誤的測試邏輯。可以請指導如何測試這種情況,最好是批量觸發測試。謝謝。因爲當代碼被破壞時,我無法將測試觸發器中的isConverted設置爲true。 – user1568642 2012-08-02 07:36:13