Ric*_*d N 5 salesforce force.com apex-code
我无法在线获得有关此问题的任何可靠信息.但我认为这一定是一个必须影响很多人的问题.
基本上我在沙盒中编写了一个简单的触发器和测试类,测试了它,当它很好的时候我将它部署到了PRD.
我首先尝试了验证模式,但是我遇到了这个错误.
System.LimitException:SOQL查询太多:101
显示此错误发生在其他一些测试类中.所以我觉得我的触发器中的测试用例运行了,再加上剩下的测试用例不知何故超出限制.
因此,我们的单元测试中的SOQL查询总数必须小于100.这有点难以正确吗?我可以想象有这么多测试用例,我们肯定需要超过100个查询.
那么有什么方法可以避免达到这个限制,因为Salesforce在部署一行代码时会运行所有测试用例.
我没有任何常见的嫌疑人......就像for循环中的SOQL一样.
更新:2012年8月19日:我现在发布测试类和触发器的源代码
测试类:
@isTest
Run Code Online (Sandbox Code Playgroud)
private class 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)
{
}
}
Run Code Online (Sandbox Code Playgroud)
}
触发
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.');
}
}
Run Code Online (Sandbox Code Playgroud)
}
你能分享一下你的想法吗?
谢谢,
卡尔文
Joh*_*ago 10
查看一些测试类会有所帮助,但需要注意的一件重要事情是需要使用Test.startTest()和Test.stopTest()方法.我们的想法是,您在设置测试时所做的任何查询或DML操作都应该在Test.startTest()方法之前关闭.然后在测试代码时,例如执行一个方法,你正在测试在start和stop方法调用之间进行测试.
这给出了单元测试上下文.这基本上会忽略在开始和停止测试之外完成的任何dml或查询,并且只计算测试中发生的事件.否则,所有设置代码和实际测试代码都被视为相同上下文的一部分,因此需要计入限制.
这个链接应该为这个主题提供一些额外的信息:http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
| 归档时间: |
|
| 查看次数: |
15983 次 |
| 最近记录: |