0

tl;dr - 如何让测试类等到特定触发器完成更新或插入后再继续测试?

背景

我有一个触发器,它在创建Portal_Content__cAccount 对象时创建一个新对象,然后将两个记录相互关联(请参阅下面的Account Trigger)。

我还在Portal_Content__c删除记录时触发的对象上创建了一个触发器。删除后,触发器会查找相关Account记录并将其删除(请参阅下面的门户内容触发器)。

我的问题是关于我的门户内容测试类(下面的最终代码块)。当我在客户记录上运行 DML 插入时,客户触发器a.Portal_Content_Record__c应该使用在客户触发器中创建的门户内容记录填充该字段。但是,当我运行system.debug()测试时,它显示a.Portal_Content_Record__c运行 SOQL 查询时该字段为空。我已经能够通过匹配名称搜索门户内容记录来绕过此问题,但我想通过按确切 ID 搜索来使脚本更加健壮。

问题

如何让Portal Content Test Class等到Account Trigger填充该a.Portal_Content_Record__c字段后再继续测试?

--代码块--

帐户触发器

trigger AccountHandler on Account (before insert, after insert, after update, after delete) {

    List<Account> alist = Trigger.New;
    List<Account> oldlist = Trigger.Old;

//  Create new Portal Content with same name as Account if Account is record type 'College/University,'
//  then assign newly created Portal Content to 'Portal Content Record' lookup field on new Account
    if(Trigger.isBefore && Trigger.isInsert){

        for(Account a : alist) {

            if (a.RecordTypeId == '012i0000001Iy1H') {
                Portal_Content__c p = new Portal_Content__c(
                    Name=a.Name,
                    RecordTypeId='012i0000001J1zZ'
                );
                insert p;

                a.Portal_Content_Record__c = p.Id;
            }
        }
    }

//  Delete Portal Content record referenced in an Account's 'Portal Content Record' lookup field
//  if the Account is deleted

    if (Trigger.isAfter && Trigger.isDelete){
        for(Account a : oldlist){
            for(Portal_Content__c p : [SELECT ID FROM Portal_Content__c WHERE ID = :a.Portal_Content_Record__c]){
                delete p;
            }
        }

    }

//  After the new Portal Content record has been created, assign the Account ID of the Account that created it
//  to the 'School SFDC ID' field on the new Portal Content record.    
    if (Trigger.isAfter && Trigger.isInsert){
        for(Account a : alist){

            List<Portal_Content__c> plist = [SELECT ID FROM Portal_Content__c WHERE Id = :a.Portal_Content_Record__c];

            for(Portal_Content__c p : plist){
                p.School_SFDC_ID__c = a.Id;
                update p;
            }

        }
    }

//  Prevent more than one Account from being assigned to a single Portal Content record
    if (Trigger.isAfter && Trigger.isUpdate) {
        for(Account a : alist){
            if (a.Portal_Content_Record__c != null){
                List<Account> alist = [SELECT ID FROM Account WHERE Portal_Content_Record__c = :a.Portal_Content_Record__c];
                system.debug('alist: ' + alist);
                if (alist.size() > 1) {
                    a.addError('The Portal Content record you selected is already associated with another School. Please select a different Portal Content record');
                }
            }
        }
    }
}

门户内容触发器

trigger PortalContentHandler on Portal_Content__c (before insert, after update, after insert, after delete) {

//  If Portal Content is deleted and Account is tied to Account record, delete Account record
    if(Trigger.isAfter && Trigger.isDelete){
        List<Portal_Content__c> plist = Trigger.old;
        for(Portal_Content__c p : plist) {
            List<Account> alist = [SELECT ID FROM Account WHERE Id = :p.School_SFDC_ID__c];
            for(Account a : alist){
                delete a;
            }       
        }
    }

//  If more than one Portal Content with the same name, prevent new Portal Content record from being created
    else if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert)){
        List<Portal_Content__c> plist = Trigger.New;
        for(Portal_Content__c p : plist){
            List<Portal_Content__c> pquery = [SELECT ID FROM Portal_Content__c WHERE Name = :p.Name];
            if(pquery.size() > 1){
                p.addError('There is already a Portal Content record with this name. Please select a different name.');
            }
        }
    }
}

门户内容测试班

@isTest
public class PortalContentHandlerTest {

    @isTest static void createThenDeletePortalContent(){
        Account a = new Account(Name='deletePortalTest',RecordTypeId='012i0000001Iy1H');
        insert a;

        List<Portal_Content__c> plist = [SELECT ID FROM Portal_Content__c WHERE Name = :a.Name];//Id = :a.Portal_Content_Record__c];

        system.debug('Delete Info: a.Id = ' + a.Id + ', Portal_Content_Record = ' + a.Portal_Content_Record__c+ ', plist = ' + plist);
        for(Portal_Content__c p : plist){
            delete p;            
        }
        system.debug('Delete Info: a.Id = ' + a.Id);
        List<Account> checklist = [SELECT ID FROM Account WHERE Id = :a.Id];
        system.debug(checklist);
        system.assertEquals(0, checklist.size());
    }
4

1 回答 1

0

我已经在我的 Org. 请在下面找到我的评论。

  • 根据您的要求,我有一个触发器,它在创建 Account 对象时创建一个新的 Portal_Content__c 对象,然后将两个记录相互关联(请参阅下面的 Account Trigger)。 我看到和
    上有关系。您正在设置. 但是您也需要链接来满足您的要求。请找到以下代码Account(Portal_Content_Record__c )Portal Content (School_SFDC_ID__c)Portal_Content_Record__cSchool_SFDC_ID__c

    trigger AccountHandler on Account (before insert, after insert, after update, after delete) {
    
            List<Account> alist = Trigger.New;
            List<Account> oldlist = Trigger.Old;
    
        //  Create the new Portal Content with the same name as Account if Account is record type College/University,
        //  then assign newly created Portal Content to Portal Content Record lookup field on new Account
            if(Trigger.isBefore && Trigger.isInsert){
    
                for(Account a : alist) {
    
                    if (a.RecordTypeId == '012i0000001Iy1H') {
                        Portal_Content__c p = new Portal_Content__c(
                            Name=a.Name,
                            RecordTypeId='012i0000001J1zZ',
                            School_SFDC_ID__c = a.id
                        );
                        insert p;
    
                        a.Portal_Content_Record__c = p.Id;
                    }
                }
            }
        }
    
  • 我看到唯一的事情是delete没有被覆盖, PortalContentHandler因为当您创建门户内容时,它不符合 List<Account> alist = [SELECT ID FROM Account WHERE Id = :p.School_SFDC_ID__c]; 填充它的条件,如上点所述。

  • 此外,您需要更改您的测试类,因为没有您尝试查询的门户内容数据。在测试类中,您只获得您在那里创建的数据。AccountHandler 创建的那个不会成为您的测试类的一部分。创建与您在触发器中创建的数据相同的数据。

    @isTest 
    public class PortalContentHandlerTest {
    
    @isTest static void createThenDeletePortalContent(){
        Account a = new 
        Account(Name='deletePortalTest',RecordTypeId__c='012i0000001Iy1H');
        insert a;
        Portal_Content__c p = new Portal_Content__c(
            Name=a.Name,
            RecordTypeId__c='012i0000001J1zZ',
            School_SFDC_ID__c = a.id
        );
        insert p;
        delete p; 
    }
    
    }
    
于 2019-03-01T12:43:58.267 回答