• kegabol rai
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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.