function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tatiana Cooke 9Tatiana Cooke 9 

When trying to test person account insertion: System.AssertException: Assertion Failed: Expected: 2, Actual: 1

Team, 

keep getting errors when trying to insert an account for a test class. 

Error: 
System.AssertException: Assertion Failed: Expected: 2, Actual: 1

New to coding so don't understand the error. I am trying to set up trigger that creates a change log record every time the person account email is changed. 
@isTest
private class TestPersonAccountChangeLog {

    static testMethod void LogPersonAccountChangeTest() {
        
        RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Ward Residential Buyers' and SObjectType = 'Account'];
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
        System.runAs ( thisUser ) {
        
            WE_Process__c obj=new WE_Process__c();
    obj.Name='test';
    insert obj;
    
            Account a = NEW Account();
            a.FirstName='John';
            a.LastName='DoeTest1';
            a.PersonEmail = 'doetest1@test.com';
            a.RecordType = personAccountRecordType;
            insert a;
            
    System.assertEquals(2, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :A.PersonEmail AND pi__ObjectState__c = 1]);
    
    delete A;
    System.assertEquals(0, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :A.PersonEmail AND pi__ObjectState__c = 2]);
    
    undelete A;
    System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :A.PersonEmail AND pi__ObjectState__c = 3]);
    }

}}

 
Amit Chaudhary 8Amit Chaudhary 8
Look like some issue in your Assert Statment. Please post you main class as well so that we can check what is the issue.

Just comment the assert that will work. But that is not a prefect solution
pconpcon
Based on the test you have, you are expecting to have two pi__ObjectChangeLog__c entries after you have you have done your DML.  But you are only getting one back.  Can you please include the Trigger code that you are trying to test?
Tatiana Cooke 9Tatiana Cooke 9
Team, 

Here is my code: 
 
trigger LogPersonAccountChange on Account (before delete, after insert, after undelete)
{
  List<pi__ObjectChangeLog__c> logs = new List<pi__ObjectChangeLog__c>(); 

  if (Trigger.new != null) {
    for (Account account : Trigger.new) {

      if (Account.PersonEmail != null && Account.PersonEmail != '') {
        pi__ObjectChangeLog__c log = new pi__ObjectChangeLog__c();
        log.pi__ObjectFid__c = Account.PersonContactId;
        log.pi__ObjectType__c = 1;
        log.pi__ObjectEmail__c = Account.PersonEmail;

        if  (System.Trigger.isInsert) {
          log.pi__ObjectState__c = 1;
        } else if  (System.Trigger.isDelete) {
          log.pi__ObjectState__c = 2;
        } else if  (System.Trigger.isUnDelete) {
          log.pi__ObjectState__c = 3;

        }
        logs.add(log);
      }
    }
  } else if (Trigger.old != null) {
    for (Account account : Trigger.old) {
      if (Account.PersonEmail != null && Account.PersonEmail != '') {
        pi__ObjectChangeLog__c log = new pi__ObjectChangeLog__c();

        log.pi__ObjectFid__c = Account.PersonContactId

        ;
        log.pi__ObjectType__c = 1;
        log.pi__ObjectEmail__c = Account.PersonEmail;
        if  (System.Trigger.isInsert) {
          log.pi__ObjectState__c = 1;
        } else if  (System.Trigger.isDelete) {
          log.pi__ObjectState__c = 2;
        } else if  (System.Trigger.isUnDelete) {
          log.pi__ObjectState__c = 3;
        }
        logs.add(log);
      }
    }
  }

  if (logs.size() > 0) {

    insert logs;
  }
}

Thank you for helping! 
pconpcon
Your asserts are not correct.  According to your trigger code, you should only have one pi__ObjectState__c record when you insert your account.  If you think you should have two then you need to update your trigger accordingly.
Deepak GulianDeepak Gulian
@isTest
private class TestPersonAccountChangeLog {

    static testMethod void LogPersonAccountChangeTest() {
        
        RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Ward Residential Buyers' and SObjectType = 'Account'];
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
        System.runAs ( thisUser ) {
        
            WE_Process__c obj=new WE_Process__c();
    obj.Name='test';
    insert obj;
    
            Account a = NEW Account();
            a.FirstName='John';
            a.LastName='DoeTest1';
            a.PersonEmail = 'doetest1@test.com';
            a.RecordType = personAccountRecordType;
            insert a;
          
//As your trigger fire it insert only one record in pi_ObjectChangeLog__c where ObjectState__c = 1  
    System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :A.PersonEmail AND pi__ObjectState__c = 1]);
    
    delete A;
    System.assertEquals(0, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :A.PersonEmail AND pi__ObjectState__c = 2]);
    
    undelete A;
    System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :A.PersonEmail AND pi__ObjectState__c = 3]);
    }

}}

This will work for you try this!
Tatiana Cooke 9Tatiana Cooke 9

Thank you!

Now that I updated the code it is saying Error: 
System.AssertException: Assertion Failed: Expected: 0, Actual: 1

On the second assertion =  (. Any ideas?

Tatiana Cooke 9Tatiana Cooke 9
Updated code to below: 

Error: 
System.ListException: List index out of bounds: 0
 
*/@isTest
private class TestPersonAccountChangeLog {

    static testMethod void LogPersonAccountChangeTest() {
        
        RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Ward Residential Buyers' and SObjectType = 'Account'];
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
        System.runAs ( thisUser ) {
        
            WE_Process__c obj=new WE_Process__c();
    obj.Name='test';
    insert obj;
    
            Account a = NEW Account();
            a.FirstName='John';
            a.LastName='DoeTest1';
            a.PersonEmail = 'doetest1@test.com';
            a.RecordType = personAccountRecordType;
            insert a;
          
//As your trigger fire it insert only one record in pi_ObjectChangeLog__c where ObjectState__c = 1  
        System.assertEquals([SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :A.PersonEmail AND pi__ObjectState__c = 1], 1);
        delete A;
        System.assertEquals([SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :A.PersonEmail AND pi__ObjectState__c = 2], 1);
        undelete A;
        System.assertEquals([SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :A.PersonEmail AND pi__ObjectState__c = 3], 1);
    }

}}

 
Deepak GulianDeepak Gulian
@isTest 
private class TestPersonAccountChangeLog { 
  
          static testMethod void LogPersonAccountChangeTest() { 
                    
                    Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
                    User thisUser = new User(Alias = 'tCook', Email='tatianaCooke@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='tCooke91@testorg.com'); //Changed

                    RecordType personAccountRecordType = [SELECT Id FROM RecordType WHERE Name = 'Ward Residential Buyers' and SObjectType = 'Account']; 

                    System.runAs ( thisUser ) { 
                               WE_Process__c obj=new WE_Process__c();
                               obj.Name='test';
                               insert obj; 

                               Account a = new Account(); 
                               a.FirstName='John'; 
                               a.LastName='DoeTest1'; 
                               a.PersonEmail = 'doetest1@test.com';
                               a.RecordTypeId = personAccountRecordType.Id; //Changed
                               insert a; 

                               System.assertEquals([SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :a.PersonEmail AND pi__ObjectState__c = 1], 1); 

                               delete a; 
                               System.assertEquals([SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :a.PersonEmail AND pi__ObjectState__c = 2], 0); 

                               undelete a; 
                               System.assertEquals([SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :a.PersonEmail AND pi__ObjectState__c = 3], 1);

                    }
          }
 }
Just tested it in my org and got 100% coverage.
 
Tatiana Cooke 9Tatiana Cooke 9
Deepak Thank you! , 

Got the following error: 
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, entity type cannot be inserted: Person Account: []

How do I troubleshoot why it won't insert?
Deepak GulianDeepak Gulian
It seems standard user dont have permission to create person account.
Replace 6th line with the below
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
Tatiana Cooke 9Tatiana Cooke 9
Deepak, 

Thank you so much for your continued help and support. I have another meeting today with the business and I want to report that this is completed. 

The system administrator changes worked but now I am getting the below error:

System.QueryException: List has no rows for assignment to SObject. 

How do I resolve?