You need to sign in to do that
Don't have an account?
Anton | De Ondernemer
Testing Apex Triggers
So I want to write better test classes and started doing the trailhead. I've just completed the following trailhead step:
https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/apex_testing/units/apex_testing_triggers
Somehow I successfully completed the step with the testclass below.
Both "block"-methods keep failing when running all tests. It fails because of the error thrown, but isnt that exactly what I'm testing for so the tests should pass? (I expect an error and I got it).
TLDR:
is a failed test "good" when testing errors or should I write my test methods differently so it passes?
https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/apex_testing/units/apex_testing_triggers
Somehow I successfully completed the step with the testclass below.
Both "block"-methods keep failing when running all tests. It fails because of the error thrown, but isnt that exactly what I'm testing for so the tests should pass? (I expect an error and I got it).
TLDR:
is a failed test "good" when testing errors or should I write my test methods differently so it passes?
@isTest private class TestRestrictContactByName { @isTest static void allowContactInsert() { Contact contact = new Contact(LastName='LastName'); Test.startTest(); Database.UpsertResult result = Database.upsert(contact); Test.stopTest(); System.assert(result.isSuccess()); } @isTest static void blockContactInsert() { Contact contact = new Contact(LastName='INVALIDNAME'); Test.startTest(); Database.UpsertResult result = Database.upsert(contact); Test.stopTest(); System.assert(!result.isSuccess()); System.assert(result.getErrors().size() > 0); System.assertEquals('The Last Name '+contact.LastName+' is not allowed for DML', result.getErrors()[0].getMessage()); } @isTest static void allowContactUpdate() { Contact contact = new Contact(LastName='LastName'); insert contact; contact.LastName = 'AdjustedLastName'; Test.startTest(); Database.upsertResult result = Database.upsert(contact); Test.stopTest(); System.assert(result.isSuccess()); } @isTest static void blockContactUpdate() { Contact contact = new Contact(LastName='LastName'); insert contact; contact.LastName = 'INVALIDNAME'; Test.startTest(); Database.UpsertResult result = Database.upsert(contact); Test.stopTest(); System.assert(!result.isSuccess()); System.assert(result.getErrors().size() > 0); System.assertEquals('The Last Name '+contact.LastName+' is not allowed for DML', result.getErrors()[0].getMessage()); } }