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
Guilherme TavaresGuilherme Tavares 

Assert Fails on Trigger Test Class

I'm trying to test a trigger that updates the Case Status field, but the assert is failing on Test Class, can anyone help me with this? 

Here is the Test Class
@isTest 
private class AtualizaStatusDoChamadoTest {
    
    private static testmethod void testaTrigger(){
        Case novoCaso = new Case();
        novoCaso.Nome__c = 'Teste';
        novoCaso.Status = 'Em aberto';
        novoCaso.Email__c = 'teste@teste.com';
        insert novoCaso;

        Comentario_caso__c novoComentario = new Comentario_caso__c();
        novoComentario.Caso__c = novoCaso.Id;
        novoComentario.Tipo_de_Comentario__c = 'Encerramento';
        insert novoComentario;   
        
        Case caso = [SELECT Id, Status FROM Case WHERE Id =: novoComentario.Caso__c];
        
        Test.startTest();
        System.assertEquals('Encerrado', caso.Status);
        Test.stopTest();
    }
}

Here is the Trigger
trigger AtualizaStatusDoChamado on Comentario_Caso__c (before insert, before update) {
    if(Trigger.isBefore){
        if(Trigger.isInsert || Trigger.isUpdate){
            List<Comentario_Caso__c> listaDeComentarios = trigger.new;
            Comentario_Caso__c novoComentario = listaDeComentarios[0];
            Case casoDoComentario = [SELECT Id, Status FROM Case WHERE Id =: novoComentario.Caso__c];
            if(novoComentario.Tipo_de_Comentario__c == 'Encerramento'){
                casoDoComentario.Status = 'Encerrado';
            }    
            System.debug('caso: ' + casoDoComentario);
        }
    }
}

Log
11:52:42:863 EXCEPTION_THROWN [19]|System.AssertException: Assertion Failed: Expected: Encerrado, Actual: Em aberto

On the Trigger, the debug is showing the correct Status, maybe I'm doing something wrong when trying to get the updated Case on Test Class.
Best Answer chosen by Guilherme Tavares
sachinarorasfsachinarorasf
Hi Guilherme,
As I am seeing your trigger here you are not updating the case record after updating the status of the case. In the test, you are querying the case after inserting the Comentario_Caso__c Record. So you need to update the case record after changing the status. After this, your assert will not fail.
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com

All Answers

sachinarorasfsachinarorasf
Hi Guilherme,
As I am seeing your trigger here you are not updating the case record after updating the status of the case. In the test, you are querying the case after inserting the Comentario_Caso__c Record. So you need to update the case record after changing the status. After this, your assert will not fail.
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com
This was selected as the best answer
ShirishaShirisha (Salesforce Developers) 
Hi Gulihemre,

Greetings!

I can see that the trigger is trying to insert/update the status to Encerrado in this line:

casoDoComentario.Status = 'Encerrado'; 

However,the DML statement has been missed.You might need to add insert or update DML commands to insert/update the record which is stored in the variable casoDoComentario.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
 
Guilherme TavaresGuilherme Tavares
Thank you guys! Newbie mistake.
kegabol raikegabol rai
financial working (https://financialworking.com/)
anajli sharmaanajli sharma
Jobs available ( https://jobmaldives.io ) for Maldives people.