You need to sign in to do that
Don't have an account?
Eric Delgado
My first unit test for trigger
Hi,
I am trying to create a new unit test for a trigger I am creating based on this example.
https://developer.salesforce.com/docs/atlas.en-us.196.0.sos.meta/sos/service_cloud_guides-auto_case_pop.htm
Here is the unit test I created for the trigger.
The unit test doesn't seem to test against the trigger I created above. How can I map this unit test to the trigger? Thanks.
I am trying to create a new unit test for a trigger I am creating based on this example.
https://developer.salesforce.com/docs/atlas.en-us.196.0.sos.meta/sos/service_cloud_guides-auto_case_pop.htm
trigger SOSCreateCaseCustom on SOSSession (before insert) { List<SOSSession> sosSess = Trigger.new; for (SOSSession s : sosSess) { try { Case caseToAdd = new Case(); caseToAdd.Subject = 'SOS Video Chat'; if (s.ContactId != null) { caseToAdd.ContactId = s.ContactId; } else { List<Contact> contactInfo = [SELECT Id from Contact WHERE Email = :s.AppVersion]; if (!contactInfo.isEmpty()) { caseToAdd.ContactId = contactInfo[0].Id; s.ContactId = contactInfo[0].Id; } } insert caseToAdd; s.CaseId = caseToAdd.Id; }catch(Exception e){} } }
Here is the unit test I created for the trigger.
@isTest private class SOSCreateCaseCustomTest { static testMethod void validateSOSCreateCase() { init(); Test.startTest(); String caseSubject = 'SOS Video Chat'; // set up case to add Case caseToAdd = new Case(Subject='SOS Video Chat'); insert caseToAdd; Case ca = [SELECT Subject, ContactId from Case where Subject =: caseSubject]; System.assertEquals(caseSubject, ca.Subject); Test.stopTest(); } }
The unit test doesn't seem to test against the trigger I created above. How can I map this unit test to the trigger? Thanks.
All Answers
private class test_sampleCase {
private static testMethod void testCase(){
List<Case> tstCase = createTestCase();
insert tstCase;
System.assertEquals();
}
public static Case createTestCase(){
Case c=new Case(Subject='test',contactId='Set a contact id ');
return c;
}
}
somehting like this. But since yours is before insert you dont even need to do a DML in trigger. Just set the value of the field to what to you want , and it will be handled during insert. there is no need for an explicit DML.
Try this . And modify accordingly. I have used Account object , since I did not have your custom object in place.
This is my code based on what you gave me: