2012-08-17 40 views
5

我無法在線獲得有關此問題的任何可靠信息。但我認爲這肯定是一個影響很多人的問題。Salesforce:全面避免測試類中的管理員限制

基本上我在沙箱裏寫了一個簡單的觸發器和測試類,測試了它,當它很好的時候我把它部署到PRD。

我試着先驗證模式,我得到了這個錯誤。

System.LimitException:太多SOQL查詢:101

示於一些其它測試類發生此錯誤。所以我覺得我的觸發器中的測試用例已經運行,並且這與其餘的測試用例不知何故超出了限制。

因此,我們的單元測試中SOQL查詢的總數量必須少於100個。這有點難以正確對待?我可以想象有這麼多的測試用例,我們當然需要超過100個查詢。

那麼有什麼方法可以避免遇到這個限制,因爲Salesforce在部署甚至一行代碼時運行所有的測試用例。

我沒有任何通常的嫌疑人......就像在for循環中的SOQL一樣。

更新:2012年8月19日:我現在發佈的測試類的源代碼,並引發

測試類:

@isTest 

私有類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 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.'); 
     } 
    } 

}

能否請您分享您的想法?

感謝,

卡爾文

+0

您收到此錯誤消息,因爲您的一個測試類(不是所有這些類)都超過了限速器限制。在錯誤消息中,您可以看到類名。發佈該類的代碼,以便我們可以查看它。 – mast0r 2012-08-17 17:59:41

+0

實際上,所顯示的類是一個在PRD中的類,目前測試用例工作正常。正如約翰在下面回答的,也許是因爲我沒有使用startTest()和stopTest()把我的代碼放在測試類中。 – 2012-08-17 20:10:22

回答

10

這將有助於看到一些你的測試類,但要注意的一件重要的事情是,你需要利用到Test.startTest()和Test.stopTest的()方法。這個想法是,你設置你的測試的任何查詢或DML操作應該在Test.startTest()方法之前。然後,當測試你的代碼,比如執行一個方法時,你正在測試的是在啓動和停止方法調用之間進行的。

這給出了單元測試的上下文。這將基本上忽略在開始和停止測試之外完成的任何dml或查詢,並且只計算在作爲測試的一部分之間發生的事情。否則,所有設置代碼和實際測試代碼都將被視爲同一上下文的一部分,因此可能會被計入限制範圍。

這個環節應該擺脫一些關於這個問題額外的光:http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

+0

當然,這是我剛纔提出的一些建議。我將在測試課中添加這兩個語句。謝謝。 – 2012-08-17 20:09:12

+0

嘿約翰,我將這些語句添加到我的測試類中,並沒有幫助。現在我已經在上面的原始文章中發佈了測試類和觸發器的代碼。 – 2012-08-19 23:51:32

+0

標記爲答案。使用force.com IDE,我可以在另一個課程中找出問題。我在適當的地方使用了starttest和stoptest。 – 2012-08-21 21:19:10

1

另一件事要記住的是,你在你的for循環執行SOQL。你可以創建一個列表並在你的循環之前存儲你的查詢結果。這樣你就不會面臨州長限制的問題,因爲你每個事務只使用一個SOQL語句。

+0

是的,這是我繼續查找編碼中的任何其他錯誤之前檢查的第一件事。 – 2012-08-29 18:12:01

+0

謝謝理查茲..!你能檢查這個問題嗎?謝謝 !! http://stackoverflow.com/q/12183564/1633936 – JeyJim 2012-08-29 18:55:21

+0

我已回覆...讓我知道如果你需要更多的幫助。 – 2012-08-29 22:20:38