Non sono stato in grado di ottenere informazioni affidabili su questo problema online. Ma penso che debba essere un problema che deve interessare molte persone.Salesforce: evitare i limiti del governatore nelle classi di test su tutta la linea
Fondamentalmente ho scritto una semplice classe trigger e test in sandbox, l'ho testata e quando è andata bene l'ho distribuita a PRD.
Ho provato prima la modalità di convalida e ho ricevuto questo errore.
System.LimitException: Troppi query SOQL: 101
Questo errore è stato mostrato a verificarsi in qualche altra classe di test. Quindi ritengo che i test case del mio innesco funzionino e questo, unito ai restanti casi test, ha in qualche modo superato il limite.
Quindi il numero totale di query SOQL nei nostri test di unità deve essere inferiore a 100. Questo è un po 'difficile da seguire? Posso immaginare con così tanti casi di test, sicuramente avremo bisogno di più di 100 query.
Quindi, quali sono i modi per evitare di superare questo limite perché Salesforce esegue tutti i test case quando distribuisce anche una singola riga di codice.
Non ho nessuno dei soliti sospetti ... come SOQL all'interno di un ciclo for.
UPDATE: 2012/08/19: Ora sto postando il codice sorgente della classe di test e innescare
di prova Classe:
@isTest
classe privata TestAccountDuplicateWebsiteTrigger {
static testMethod void myUnitTest() {
try{
// TO DO: implement unit test
Test.startTest();
Account a1;
a1 = new Account();
a1.name = 'GMSTest';
a1.Website = 'www.test.com';
Account a2;
a2 = new Account();
a2.name = 'GMSTest2';
a2.Website = 'www.test.com';
Account a3;
a3 = new Account();
a3.name = 'GMSTest3';
a3.Website = 'www.test1.com';
insert a1;
insert a2;
//insert a3;
Test.stopTest();
}
catch (Exception e)
{
}
}
}
trigger
trigger osv_unique_website_for_account on Account (before insert, before update) {
//Map which has no duplicates with website as the key
Map<String, Account> accountMap = new Map<String, Account>();
for (Account account: System.Trigger.new)
{
//Ensure that during an update, if an website does not change - it should not be treated as a duplicate
if ((account.Website != null) && (System.Trigger.isInsert ||
(account.Website != System.Trigger.oldMap.get(account.Id).Website)))
{
//check for duplicates among the new accounts in case of a batch
if (accountMap.containsKey(account.Website))
{
account.Website.addError('Cannot save account. Website already exists.');
}
else
{
accountMap.put(account.Website, account);
}
}
}
//Now map containing new account websites has been created.
//Check them against the account websites that ALREADY EXIST in salesforce. If website exists, display error.
for (Account account : [SELECT Website FROM Account WHERE Website IN :accountMap.KeySet()])
{
Account newAccount = accountMap.get(Account.Website);
if (newAccount!=null)
{
newAccount.Website.addError('Cannot save account. Website already exists.');
}
}
}
Potete per favore condividere i tuoi pensieri?
Grazie,
Calvin
Si ottiene questo messaggio di errore perché una delle classi di test (non tutte!) Supera il limite del governor. Nel messaggio di errore puoi vedere Il nome della classe. Pubblica il codice di quella classe qui in modo che possiamo verificarlo. – mast0r
In realtà la classe mostrata è una classe che si trova in PRD e attualmente i casi di test funzionano correttamente. Come John ha risposto qui sotto, forse è successo perché non ho usato startTest() e stopTest() per racchiudere il mio codice nella classe di test. –