0

I created a test class to test a trigger and I am getting the following error when trying to deploy the change set. I don't know if the error is with the Test or the change set, error below:

System.AssertException: Assertion Failed Stack Trace: Class.Test_AutoApprove.t1: line 11, column 1

Test is as follows:

@isTest
private class Test_AutoApprove {
static testmethod void t1(){
        Site_Call_Performance__c t = new Site_Call_Performance__c(Status__c=       'Submitted');

test.startTest();
insert t;
update t;
test.stopTest();

System.assert(t.Status__c == 'Approved');
}
}

Trigger!

trigger AutoApprovebasedoffCheckbox on Site_Call_Performance__c (after update) 
{
List<Approval.ProcessSubmitRequest> reqList = new List<Approval.ProcessSubmitRequest>();
Set<ID> scpIDs = new Set<ID>();
for (Site_Call_Performance__c a : Trigger.new)
{
    if (a.Approval_Checkbox__c)
    {
        scpIDs.add(a.Id);
    }
}

Set<Id> pIds = (new Map<Id, ProcessInstance>([SELECT Id,Status,TargetObjectId FROM ProcessInstance where Status='Pending' and TargetObjectId in :scpIds ])).keySet();
Set<Id> pInstanceWorkitems = (new Map<Id, ProcessInstanceWorkitem>([SELECT Id,ProcessInstanceId FROM ProcessInstanceWorkitem WHERE ProcessInstanceId in :pIds])).keySet();

List<Approval.ProcessWorkItemRequest> approve = New List<Approval.ProcessWorkItemRequest>(); 

for (Id pInstanceWorkitemsId : pInstanceWorkitems)
{
    Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
    req.setComments('Auto Approved by System');
    req.setAction('Approve'); 
    req.setWorkitemId(pInstanceWorkitemsId);

    approve.add(req);
}

List<Approval.ProcessResult> result = Approval.process(approve);

}  
0

Assuming you're testing a trigger, you need to remember to query the database for any changes to the record. This simple change should work (assuming your trigger works):

test.stopTest();
t = [select status__c from Site_Call_Performance__c where id = :t.Id];
System.assertEquals('Approved', t.Status__c);

Note: I recommend assertEquals because it prints a friendly message like "Assertion Failed. Expected: Approved, Actual: Not Approved".

| improve this answer | |
  • ok, maybe I am doing this wrong. I added another field value to the record and it is still not working! I also used added your suggestions, i think has to be my test because the code works as intended in sandbox. – Jeff Green Aug 15 '17 at 20:17
  • @JeffGreen Did you also check to make sure you included the trigger and any supporting classes? – sfdcfox Aug 15 '17 at 20:19
  • i did, i am very new at this! This is the trigger that works in sandbox, is this not the right test for it? I am getting 60% code coverage. – Jeff Green Aug 15 '17 at 20:20
  • @JeffGreen Okay, did you include the correct approval processes, maybe? – sfdcfox Aug 15 '17 at 20:24
  • I was planning on using the approval processes in production, do i need to include approval processes? – Jeff Green Aug 15 '17 at 20:26
0

This is error when test class runs and according to assert condition it will always fail.

There could be some other process exist which could update the status as Approved.

So, better to fetch the record from database and compare the results.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.