Triggers 插入前/更新前Apex触发器上的测试覆盖率失败
我有一个非常简单的Prefore insert/update触发器on Opportunity,它根据包含销售办事处(州)位置信息的下拉值自动选择价目表 这是我的触发器:Triggers 插入前/更新前Apex触发器上的测试覆盖率失败,triggers,salesforce,code-coverage,apex-code,Triggers,Salesforce,Code Coverage,Apex Code,我有一个非常简单的Prefore insert/update触发器on Opportunity,它根据包含销售办事处(州)位置信息的下拉值自动选择价目表 这是我的触发器: trigger SelectPriceBook on Opportunity ( before insert, before update ) { for( Opportunity opp : Trigger.new ) { // Change Price Book // New Yo
trigger SelectPriceBook on Opportunity ( before insert, before update ) {
for( Opportunity opp : Trigger.new ) {
// Change Price Book
// New York
if( opp.Campus__c == 'NYC' )
opp.Pricebook2Id = PB_NYC; // contains a Pricebook's ID
// Atlanta
if( opp.Campus__c == 'ATL' )
opp.Pricebook2Id = PB_ATL; // contains another Pricebook's ID
}
}
@isTest (SeeAllData = true)
public class SelectPriceBookTestClass {
static testMethod void validateSelectPriceBook() {
// Pricebook IDs
ID PB_NYC = 'xxxx';
ID PB_ATL = 'xxxx';
// New Opp
Opportunity opp = new Opportunity();
opp.Name = 'Test Opp';
opp.Office__c = 'NYC';
opp.StageName = 'Quote';
// Insert
insert opp;
// Retrive inserted opportunity
opp = [SELECT Pricebook2id FROM Opportunity WHERE Id =:opp.Id];
System.debug( 'Retrieved Pricebook Id: ' + opp.Pricebook2Id );
// Change Campus
opp.Office__c = 'ATL';
// Update Opportunity
update opp;
// Retrive updated opportunity
opp = [SELECT Pricebook2id FROM Opportunity WHERE Id =:opp.Id];
System.debug( 'Retrieved Updated Pricebook Id: ' + opp.Pricebook2Id );
// Test
System.assertEquals( PB_ATL, opp.Pricebook2Id );
}
}
这是我的测试课:
trigger SelectPriceBook on Opportunity ( before insert, before update ) {
for( Opportunity opp : Trigger.new ) {
// Change Price Book
// New York
if( opp.Campus__c == 'NYC' )
opp.Pricebook2Id = PB_NYC; // contains a Pricebook's ID
// Atlanta
if( opp.Campus__c == 'ATL' )
opp.Pricebook2Id = PB_ATL; // contains another Pricebook's ID
}
}
@isTest (SeeAllData = true)
public class SelectPriceBookTestClass {
static testMethod void validateSelectPriceBook() {
// Pricebook IDs
ID PB_NYC = 'xxxx';
ID PB_ATL = 'xxxx';
// New Opp
Opportunity opp = new Opportunity();
opp.Name = 'Test Opp';
opp.Office__c = 'NYC';
opp.StageName = 'Quote';
// Insert
insert opp;
// Retrive inserted opportunity
opp = [SELECT Pricebook2id FROM Opportunity WHERE Id =:opp.Id];
System.debug( 'Retrieved Pricebook Id: ' + opp.Pricebook2Id );
// Change Campus
opp.Office__c = 'ATL';
// Update Opportunity
update opp;
// Retrive updated opportunity
opp = [SELECT Pricebook2id FROM Opportunity WHERE Id =:opp.Id];
System.debug( 'Retrieved Updated Pricebook Id: ' + opp.Pricebook2Id );
// Test
System.assertEquals( PB_ATL, opp.Pricebook2Id );
}
}
测试运行报告0%测试覆盖率
同样,在类似的行中,我有另一个before insert触发器,它将事件的所有者设置为与父潜在客户的所有者相同。代码如下:
trigger AutoCampusTourOwner on Event( before insert ) {
for( Event evt : Trigger.new ) {
// Abort if other kind of Event
if( evt.Subject != 'Visit' )
return;
// Set Owner Id
Lead parentLead = [SELECT OwnerId FROM Lead WHERE Id = :evt.WhoId];
evt.OwnerId = parentLead.OwnerId;
}
}
这也导致了0%的覆盖率——我猜这与这两个循环中的for循环有关。我知道我在for循环中调用SOQL查询是在严重藐视DML规则,但出于我的目的,这应该是好的,因为这些事件是手动创建的,并且一次只创建一个,所以不会因为批量插入而影响调控器限制的范围
这两种情况下的代码都能100%工作。请建议对测试用例进行修复 你试过扳机吗??我的想法是,当您将测试类中的office从NYC更新为ATL时,值'NYC'将位于trigger.old中,这就是您要在触发器中检查的内容。
我可能是错的,因为我也是apex的新手,但是尝试一下,让我知道会发生什么。对于第一个触发器,不要做任何事情,只是创造机会并像这样执行 SelectPriceBook的测试类
@isTest
private class TriggerTestClass {
static testmethod void selectPriceTest(){
Opportunity opps = new Opportunity(
Name= 'Test Opps',
CloseDate = System.today().addDays(30),
StageName = 'Prospecting',
ForecastCategoryName = 'Pipeline',
Office__c = 'NYC');
insert opps;
Opportunity opps2 = new Opportunity(
Name= 'Test Opps 2',
CloseDate = System.today().addDays(28),
StageName = 'Prospecting',
ForecastCategoryName = 'Pipeline',
Office__c = 'ATL');
insert opps2;
}
}
它会给你很好的测试覆盖率,我不知道你想在AutoCumpstourowner做什么 我有这方面的测试课程 在Infloones\uuuu客户端\uu电子邮件\uuuuu c上触发ClientEmailTrigger(插入后、更新后、插入前、更新前){
}在第一种情况下,您的触发器正在查看校园,但您的测试正在设置office__c@superfell对不起,这是个打字错误。但代码仍然失败。有什么建议吗?