【问题标题】:salesforce trigger code coverage stuck at 35% no matter what i do无论我做什么,salesforce 触发代码覆盖率都停留在 35%
【发布时间】:2012-08-02 15:04:24
【问题描述】:

您好,我是 saleforce 顶点编码的新手,我编写了一个触发器,它在潜在客户转换时将自定义对象传输到帐户和联系人。(交易/报价)。我已经为它编写了触发器和测试代码,但我被困在 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
// }

} }

这是我编写的用于验证代码的测试类

@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);
    }

}

}

请帮我弄清楚如何增加代码覆盖率提前谢谢你

【问题讨论】:

  • 不要写代码来提高代码覆盖率;编写提供价值的测试,即您希望代码正确/正确地做什么?编写这些测试,然后看看你的覆盖率如何提高。任何未涵盖的代码都会问自己“它有什么作用?”,“它的目的是什么?”并编写执行目的而非实现的测试。
  • 我同意@Shaun Wilde。尝试并列出将在触发器中执行不同分支的不同场景。
  • 将定义尝试这种方法..thanx 快速回复...

标签: triggers salesforce code-coverage


【解决方案1】:

我相信这是因为这部分代码没有被执行(或者更确切地说是那里的 if 条件)。

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,Shaun...触发器在沙箱中运行良好...我必须在测试类中测试 转换操作的引导,我想我是写错误的测试逻辑。请指导如何测试这种情况,最好是批量触发测试。谢谢你。原因当我破坏代码时,我无法在更新测试触发器时将 isConverted 设置为 true。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 2017-11-21
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 2022-11-14
  • 2022-12-16
相关资源
最近更新 更多