我以71%的速度为71%,由于某些原因,在测试中无法运行4行代码。当我在Salesforce中测试自己时,它可以正常运行(这些代码行正在运行)。如何在测试中获得这些代码行?
-
线不运行,以循环为名 nextID = integer.valueof(c.next_id__c);
-
线未运行,在第三次循环中 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 ;
}
}
即使使用seealldata = true增加了测试覆盖范围,但除非并且直到真正需要之前,否则使用seealldata是最好的做法。请在此处找到博客以获取详细信息。
增加覆盖范围的另一种方法是为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;
}
}
我添加了下面的行,因此,当循环之前执行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;
只是一个观察,最好避免soql查询以避免运行时异常( 101:太多SOQL查询)
@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;
}
}
添加@istest(seealldata = true),然后移动到100% https://developer.salesforce.com/forums/forumsmain?id=9060g00000000i5f8