I'm working on the APEX TRIGGER TESTING challenge , and I validate the challenge with a very bad code (to be honest, I saw that I got 100% coverage and I was really surprised, try to validate and the challenge pass),
I'd like to understand why it passed because there is many things I don't understand ;
Here is my code :
@isTest
private class TestRestrictContactByName {
@TestSetUp
Static void SetUp() {
// en fait non utilisé mais bon pour entrainement
}
Static testmethod void TestAjoutListe () {
// le premier contact doit passer, le second non
List <Contact> contList2 = new List <Contact> {
new Contact(firstName='Al', lastName='Douz'),
new Contact(firstName='DonneeTest', lastName='INVALIDNAME')};
// perform test : tentative d'upsert
Test.startTest();
Database.UpsertResult[] UpResult = database.upsert(contList2, false);
Test.stopTest();
// on boucle sur les resultats
// On doit pouvoir boucler sur les bons resultats ?
// Comme on est en test, pas sur, car la query ne fonctionne pas
for (Database.UpsertResult dr : UpResult) {
if(dr.isSuccess()!=true) {
System.debug('Si tu passes ici c\'est bien que l\'opération n\'est pas un succes');
}
// verif de la taille de la liste
// System.debug('Taille liste err ' + UpResult. getErrors().size());
//System.assert(UpResult.getErrors().size() = 0);
System.debug('Taille liste');
}
}
}
So my questions are : - how can people only test with one contact, that is INVALID ; don't we have to test if the trigger doesn't interface with a good insertion ?
- also, all the code I can find on the internet (e.g. here or here), only test the INSERT (what about the before update trigger ??) ?!
for the assertEquals stuff, I've to read the documentation before asking ; I think I still have an issue understanding how works the Database.SaveResult (or Database.UpsertResult) objects ... Is there a list of lists, in which there are couples (objectToUpsert, SuccessOrNot) ?
Thanks again !