• Ashley Brinkmeyer
  • NEWBIE
  • 15 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

Hi everyone! I read through the unit about testing Apex triggers and feel like I had a pretty good understanding, but the code I wrote for the challenge test isnt running like I thought it would. Here is what I have:

@isTest
private class TestRestrictContactByName {

    @isTest static void metodoTest() 
    {
    
        
        
        Contact c = new Contact(FirstName='James', LastName = 'INVALIDNAME',email='Test@test.com');
        

        
        // Perform test
        Test.startTest();
        Database.SaveResult result = Database.insert(c);
        Test.stopTest();
        

        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() > 0);
        System.assertEquals('The Last Name "INVALIDNAME" is not allowed for DML',
                             result.getErrors()[0].getMessage());


        
    }
    
}
and this is the error I get when I run it:

"System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Last Name "INVALIDNAME" is not allowed for DML: []"

To me, it seems like it's throwing an error because the trigger is firing and not allowing an insert, but I don't understand why it would do that when I'm trying to test to make sure it DOES get thrown. I know I'm missing something here and I'd really appreciate some insight. Thank you!

Hi everyone! I read through the unit about testing Apex triggers and feel like I had a pretty good understanding, but the code I wrote for the challenge test isnt running like I thought it would. Here is what I have:

@isTest
private class TestRestrictContactByName {

    @isTest static void metodoTest() 
    {
    
        
        
        Contact c = new Contact(FirstName='James', LastName = 'INVALIDNAME',email='Test@test.com');
        

        
        // Perform test
        Test.startTest();
        Database.SaveResult result = Database.insert(c);
        Test.stopTest();
        

        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() > 0);
        System.assertEquals('The Last Name "INVALIDNAME" is not allowed for DML',
                             result.getErrors()[0].getMessage());


        
    }
    
}
and this is the error I get when I run it:

"System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The Last Name "INVALIDNAME" is not allowed for DML: []"

To me, it seems like it's throwing an error because the trigger is firing and not allowing an insert, but I don't understand why it would do that when I'm trying to test to make sure it DOES get thrown. I know I'm missing something here and I'd really appreciate some insight. Thank you!