我在71%,由于某种原因,4行代码无法在测试中运行。 当我在Salesforce中测试自己时,它可以运行(这些代码行正在运行)。 如何才能在测试中运行这些代码?
行没有运行,在第二个for循环中 nextId = Integer.Valueof(c.next_id__c);
行没有运行,在第三个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 ;
}
}
答案 0 :(得分:2)
尽管使用seeAllData = true可以提高测试覆盖率,但是除非确实需要,否则最好不要使用seeAllData。有关详细信息,请参阅博客here。
增加覆盖率的另一种方法是为tracking_next_id__c对象创建测试数据。
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循环之前执行2个查询(以前未涵盖)时,它将获取数据,因为我们现在已将它插入测试类中。
$html1 = file_get_html("https://www.amazon.com" .
"/Tom-Clancys-Division-Xbox-One/dp/B00DDXILBQ/" .
"ref=sr_1_1?ie=UTF8&qid=1487430751&sr=8-1&keywords=Tom%2BClancy%27s%2BThe%2BDivision&th=1");
$title1 = $html1 - > find("div.sims-fbt-rows,0") - > find("span.p13n-sc-price") - > innertext;
if ($title1) {
echo $title1;
} else {
echo 'problem';
}
只是观察一下,最好避免FOR循环中的SOQL查询以避免运行时异常( 101:SOQL查询太多)
答案 1 :(得分:0)
@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;
}
}
答案 2 :(得分:0)
添加@isTest(SeeAllData = true)并将其移至100% https://developer.salesforce.com/forums/ForumsMain?id=9060G000000I5f8