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 

Error regarding test class: "System.AssertException: Assertion Failed: Expected: 0, Actual 1"

Team, 

I have the following trigger on my person accounts and am trying to run the below test class with the following error. 

Error: "System.AssertException: Assertion Failed: Expected: 0, Actual: 1"  on line 40 of my test class.

Appreciate any guidance.

Trigger:
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;
  }
}
Here is the test class:
 
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class TestPersonAccountChangeLog {

    static testMethod void LogPersonAccountChangeTest() {
    	
String RecTypeId= [select Id from RecordType where (Name='Ward Residential Buyers') and (SobjectType='Account')].Id;
    	   
    WE_Process__c obj=new WE_Process__c();
    obj.Name='test';
    insert obj;
     
    Account account = new Account();    
    account.FirstName='Test Acc';
    account.LastName='Last Name';
    Account.PersonEmail = 'abc@123.com';
    account.RecordTypeID=RecTypeId;
    insert Account;
        
        System.assertEquals([SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 1], 1);
        delete Account;
        System.assertEquals([SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 2], 1);
        undelete Account;
        System.assertEquals([SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 3], 1);
    }

}


 
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

Create test data for pi__ObjectChangeLog__c ...

pi__ObjectChangeLog__c  p1 = new pi__ObjectChangeLog__c ();

p1.name = 'fgasfsa';

p1.pi__ObjectState__c =1;

p1.pi__ObjectEmail__c = Account.PersonEmail;

Insert p1;

Deepak GulianDeepak Gulian
Replace by this in your test class:-

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

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

undelete Account;
System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 3]);
 
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
Do both things suggested by me and Deepak
Tatiana Cooke 9Tatiana Cooke 9
Team, 

Added both suggestions 
insert Account;

	pi__ObjectChangeLog__c  p1 = new pi__ObjectChangeLog__c ();
	p1.name = 'fgasfsa';
	p1.pi__ObjectState__c = 1;
	p1.pi__ObjectEmail__c = 'Account.PersonEmail';
	p1.pi__ObjectFid__c = 'Sample Fid';
	p1.pi__ObjectType__c = 1;
	Insert p1;


	System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 1]);
	delete Account;
	System.assertEquals(0, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 2]);
	undelete Account;
	System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 3]);
	    }
but am still getting error System.AssertException: Assertion Failed: Expected: 0, Actual: 1 on the line 
System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 1]);

Any thoughts?
Deepak GulianDeepak Gulian
No need to add this in your test class

pi__ObjectChangeLog__c p1 = new pi__ObjectChangeLog__c ();
p1.name = 'fgasfsa';
p1.pi__ObjectState__c = 1;
p1.pi__ObjectEmail__c = 'Account.PersonEmail';
p1.pi__ObjectFid__c = 'Sample Fid';
p1.pi__ObjectType__c = 1;
Insert p1;
 
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
@Tatiana - remove single quotes for 
p1.pi__ObjectEmail__c = Account.PersonEmail;
Tatiana Cooke 9Tatiana Cooke 9
Taking the quote out worked now I am on the last line of code and it is not linking it with the same error: System.AssertException: Assertion Failed: Expected: 0, Actual: 1 

Below is the code line giving me issues. 
    System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c =:Account.PersonEmail AND pi__ObjectState__c = 3]);
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri
Use this please
System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c =:Account.PersonEmail AND pi__ObjectState__c = 1]);
Deepak GulianDeepak Gulian
This is your complete Test Class

/** * This class contains unit tests for validating the behavior of Apex classes * and triggers. * * Unit tests are class methods that verify whether a particular piece * of code is working properly. Unit test methods take no arguments, * commit no data to the database, and are flagged with the testMethod * keyword in the method definition. * * All test methods in an organization are executed whenever Apex code is deployed * to a production organization to confirm correctness, ensure code * coverage, and prevent regressions. All Apex classes are * required to have at least 75% code coverage in order to be deployed * to a production organization. In addition, all triggers must have some code coverage. * * The @isTest class annotation indicates this class only contains test * methods. Classes defined with the @isTest annotation do not count against * the organization size limit for all Apex scripts. * * See the Apex Language Reference for more information about Testing and Code Coverage. */

@isTest private class TestPersonAccountChangeLog {

static testMethod void LogPersonAccountChangeTest() {

String RecTypeId= [select Id from RecordType where (Name='Ward Residential Buyers') and (SobjectType='Account')].Id;
WE_Process__c obj=new WE_Process__c();
obj.Name='test'; insert obj;

Account account = new Account();
account.FirstName='Test Acc';
account.LastName='Last Name';
Account.PersonEmail = 'abc@123.com';
account.RecordTypeID=RecTypeId;

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

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

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

}
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

@Deepak - Apparently you have to consider creating test data for 
pi__ObjectChangeLog__c this cusotm object. I am pretty test class doesn't make sense without this.

Thanks

Tatiana Cooke 9Tatiana Cooke 9
ok last question I promise!!

Below is the final error I am getting. Description Resource Path Location Type

System.ListException: List index out of bounds: 0 TestPersonAccountChangeLog.cls /Howard Hughes Production/src/classes line 3570 Force.com run test failure

I don't want to mess up my code since I'm so close. can someone help me solve this pls.


 
Tatiana Cooke 9Tatiana Cooke 9
Team,

Updated my code to the below with some test data but am getting the following error. Should I be creating lists? please help! 

Error:System.AssertException: Assertion Failed: Expected: 1, Actual: 2
 
@isTest
private class TestPersonAccountChangeLog {

    static testMethod void LogPersonAccountChangeTest() {
            
String RecTypeId= [select Id from RecordType where (Name='Ward Residential Buyers') and (SobjectType='Account')].Id;
           
    WE_Process__c obj=new WE_Process__c();
    obj.Name='test';
    insert obj;

  //CREATE TEST ACCOUNT
    Account account = new Account();   
    account.FirstName='Test Acc';
    account.LastName='Last Name';
    account.PersonEmail = 'abc@123.com';
    account.RecordTypeID=RecTypeId;
    
    insert account;
    

    pi__ObjectChangeLog__c  p1 = new pi__ObjectChangeLog__c (); 

    p1.name = 'fgasfsa'; 
    p1.pi__ObjectState__c = 1; 
    p1.pi__ObjectEmail__c = Account.PersonEmail; 
    p1.pi__ObjectFid__c = 'Sample Fid'; 
    p1.pi__ObjectType__c = 1; 
    Insert p1; 

    System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 1]);
    
    delete Account;
    System.assertEquals(0, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 2]);
    
    undelete Account;
    System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 3]);
    }
}

 
Deepak GulianDeepak Gulian
At line 31 make this change
System.assertEquals(2, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 1]);
Tatiana Cooke 9Tatiana Cooke 9
Thank you Deepak, now I have this error: 

System.ListException: List index out of bounds: 0 TestPersonAccountChangeLog.cls /Howard Hughes Production/src/classes line 3570 Force.com run test failure

Below is my updated code:
 
@isTest
private class TestPersonAccountChangeLog {

    static testMethod void LogPersonAccountChangeTest() {
            
String RecTypeId= [select Id from RecordType where (Name='Ward Residential Buyers') and (SobjectType='Account')].Id;
           
    WE_Process__c obj=new WE_Process__c();
    obj.Name='test';
    insert obj;

  //CREATE TEST ACCOUNT
    Account account = new Account();   
    account.FirstName='Test Acc';
    account.LastName='Last Name';
    account.PersonEmail = 'abc@123.com';
    account.RecordTypeID=RecTypeId;
    
    insert account;
    

    pi__ObjectChangeLog__c  p1 = new pi__ObjectChangeLog__c (); 

    p1.name = 'fgasfsa'; 
    p1.pi__ObjectState__c = 1; 
    p1.pi__ObjectEmail__c = Account.PersonEmail; 
    p1.pi__ObjectFid__c = 'Sample Fid'; 
    p1.pi__ObjectType__c = 1; 
    Insert p1; 

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


Please advise,


 
Deepak GulianDeepak Gulian
System.assertEquals(2, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 1]);
    
    delete Account;
    System.assertEquals(0, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 2]);
    
    undelete Account;
    System.assertEquals(1, [SELECT COUNT() FROM pi__ObjectChangeLog__c WHERE pi__ObjectEmail__c = :Account.PersonEmail AND pi__ObjectState__c = 3]);
    }
Replace with this
 
Tatiana Cooke 9Tatiana Cooke 9
Got thie below error  again:

System.AssertException: Assertion Failed: Expected: 0, Actual: 1

What I am trying to do is set up a brand new conection between our new Pardot instance and our person accounts. 

They gave me these instructions to follow: http://help.pardot.com/customer/en/portal/articles/2127277-setting-up-salesforce-person-accounts-for-pardot-syncing?b_id=11139

What am I doing wrong? 

Do there need to be some records on the object change created? 

We only have a partial sandbox but I have created the contection between pardot and our sandbox. 

Please help! 
Fabian ManzanoFabian Manzano

Hi, this is a comment not an answer as this forum doesnt have this option, where you able to find an answer for this issue?

I start getting the same issue, the weird thing is that last week (when i did my last deployment) I didnt have this issue.