【问题标题】:salesforce How to reach 75% apex testsalesforce 如何达到 75% 的顶点测试
【发布时间】:2017-02-16 15:23:09
【问题描述】:

我在 71%,4 行代码由于某种原因无法在测试中运行。 当我在 Salesforce 中测试自己时,它可以工作(那些代码行正在运行)。 如何让这些代码行在测试中运行?

  1. 行未运行,在第二个 for 循环中 nextId=Integer.Valueof(c.next_id__c);

  2. 行未运行,在第三个 for 循环中 btnRecord.next_id__c = newid + 1; btnRecord.last_id__c = newId;

        btnRecord.last_assigned_starting_id__c = nextId;
        btnRecord.last_assigned_ending_id__c = newId;
    

下面是我的代码:

trigger getNextId on tracking__c (before insert, before update) {

Integer newId;
Integer lastId;
Integer nextId;

newId=0;
lastId=0;
nextId =0;



//add the total accounts to the last_id
for (tracking__c bt: Trigger.new) {

    //get the next id
    List<tracking_next_id__c> btnxtid = [SELECT  next_id__c FROM tracking_next_id__c];

    for (tracking_next_id__c c : btnxtid )
    {
       nextId=Integer.Valueof(c.next_id__c);
    }


    newId = Integer.Valueof(bt.total_account__c) + nextId;

    bt.starting_id__c = nextId;
    bt.ending_id__c = newId;

    tracking_next_id__c[] nextIdToUpdate = [SELECT last_id__c, next_id__c, last_assigned_starting_id__c, last_assigned_ending_id__c FROM tracking_next_id__c];
    for(tracking_next_id__c btnRecord : nextIdToUpdate ){

        btnRecord.next_id__c = newid + 1;
        btnRecord.last_id__c = newId;

        btnRecord.last_assigned_starting_id__c = nextId;
        btnRecord.last_assigned_ending_id__c = newId;

    }

    update nextIdToUpdate ;

   }
   }

【问题讨论】:

    标签: salesforce


    【解决方案1】:

    尽管使用 seeAllData=true 可以增加测试覆盖率,但除非确实需要,否则最好不要使用 seeAllData。详情请查看博客here

    另一种增加覆盖率的方法是为 tracking_next_id__c 对象创建测试数据。

        @isTest
        private class getNextIdTest {
        static testMethod void validateOnInsert(){
            tracking_next_id__c c = new tracking_next_id__c(next_id__c='Your next_id', 
                    last_id__c='Your last_id', last_assigned_starting_id__c='Your last_assigned_starting_id', 
                    last_assigned_ending_id__c='last_assigned_ending_id');
            insert c;
            tracking__c b = new tracking__c(total_account__c=Integer.Valueof(99));
            System.debug('before insert : ' + b.total_account__c);
            insert b;
            System.debug('after insert : ' + b.total_account__c);
            List<tracking__c> customObjectList =
            [SELECT total_account__c FROM tracking__c ];
            for(bid_tracking__c ont : customObjectList){
                ont.total_account__c = 5;
            }
            update customObjectList;
        }
    }
    

    我在下面添加了一行,这样,当在 FOR 循环之前执行 2 个查询时(之前没有涉及),它将获取数据,因为我们现在已将其插入到测试类中。

        tracking_next_id__c c = new tracking_next_id__c(next_id__c='Your next_id', 
                last_id__c='Your last_id', last_assigned_starting_id__c='Your last_assigned_starting_id', 
                last_assigned_ending_id__c='last_assigned_ending_id');
        insert c;
    

    只是一个观察,最好在 FOR 循环中避免 SOQL 查询以避免 Runtime Exception (101:Too many SOQL query)

    【讨论】:

      【解决方案2】:
       @isTest
      private class getNextIdTest {
              static testMethod void validateOnInsert(){
              tracking__c b = new tracking__c(total_account__c=Integer.Valueof(99));
              System.debug('before insert : ' + b.total_account__c);
          insert b;
          System.debug('after insert : ' + b.total_account__c);
          List<tracking__c> customObjectList =
       [SELECT total_account__c FROM tracking__c ];
          for(bid_tracking__c ont : customObjectList){
          ont.total_account__c = 5;
          }
          update customObjectList;
          }
          }
      

      【讨论】:

        【解决方案3】:

        添加了@isTest(SeeAllData=true) 并将其移至 100% https://developer.salesforce.com/forums/ForumsMain?id=9060G000000I5f8

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-17
          相关资源
          最近更新 更多