Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Buildsoftwaresphere/c04eb211720e9b4756819590758f48f2 to your computer and use it in GitHub Desktop.
Save Buildsoftwaresphere/c04eb211720e9b4756819590758f48f2 to your computer and use it in GitHub Desktop.
Test Apex Triggers Challenge
@isTest
private class TestRestrictContactByName {
@isTest static void testInvalidName() {
//try inserting a Contact with INVALIDNAME
Contact myConact = new Contact(LastName='INVALIDNAME');
insert myConact;
// Perform test
Test.startTest();
Database.SaveResult result = Database.insert(myConact, false);
Test.stopTest();
// Verify
// In this case the creation should have been stopped by the trigger,
// so verify that we got back an error.
System.assert(!result.isSuccess());
System.assert(result.getErrors().size() > 0);
System.assertEquals('Cannot create contact with invalid last name.',
result.getErrors()[0].getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment