You need to sign in to do that
Don't have an account?
Hung Vo 891
The 'RestrictContactByName' class did not achieve 100% code coverage via your test methods
I'm doing the "Test Apex Triggers" challenge and get this error:
"The 'RestrictContactByName' class did not achieve 100% code coverage via your test methods"
I also attach my code here, can you show me where could it be the wrong spot?
"The 'RestrictContactByName' class did not achieve 100% code coverage via your test methods"
I also attach my code here, can you show me where could it be the wrong spot?
@isTest public class TestRestrictContactByName { @isTest static void TestInsertContact_INVALIDNAME(){ Contact contact = new Contact(LastName='INVALIDNAME'); Test.startTest(); Database.SaveResult result = Database.insert(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 TestInsertContact_VALIDNAME(){ Contact contact = new Contact(LastName='VALIDNAME'); Test.startTest(); Database.SaveResult result = Database.insert(contact); Test.stopTest(); System.assert(result.isSuccess()); } @isTest(SeeAllData=true) static void TestUpdateContact_INVALIDNAME(){ Contact contact = [SELECT Id FROM Contact WHERE LastName = 'Test Contact' LIMIT 1]; contact.LastName = 'INVALIDNAME'; Test.startTest(); Database.SaveResult result = Database.update(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(SeeAllData=true) static void TestUpdateContact_VALIDNAME(){ Contact contact = [SELECT Id FROM Contact WHERE LastName = 'Test Contact' LIMIT 1]; contact.LastName = 'VALIDNAME'; Test.startTest(); Database.SaveResult result = Database.update(contact); Test.stopTest(); System.assert(result.isSuccess()); } }
After a few try out, my code worked.
The reason maybe something went wrong and I just log out and login again, and everything works fine.
Also remember to to this step
All Answers
Please use the following code. It will give you 100% code coverage.
TestRestrictContactByName.apxc
Thanks,
Try the codes below:
Trigger:
Class:
Note: After saving these two please go to developer console --> test --> run all (if you are using anything else please ensure this is done). This will solve your problem.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
After a few try out, my code worked.
The reason maybe something went wrong and I just log out and login again, and everything works fine.
Also remember to to this step