这个测试用例的目的是什么?

时间:2013-06-05 20:37:23

标签: triggers salesforce

有人能解释一下这段代码对我的影响吗?我不明白system.debug行的目的。

Test.startTest();
// 1. First check to see if it's a brand new Owner ID
System.debug('first test'); // Creating a new opportunity to start Trigger
Opportunity newtestOpp1 = TestUtil.initOpportunity(TestUtil.initAccount(),TestUtil.initContact());
User testUser1 = TestUtil.initUser(); 
newtestOpp1.OwnerId = testUser1.Id;//setting OwnerId
System.debug('The opp owner should be null' + newtestOpp1.Op_Owner__c);
try{        
    insert newtestOpp1;
} catch ( DMLException d ) {
    System.debug(d);
}
System.debug('The opp owner should not be null' + newtestOpp1.Op_Owner__c);

1 个答案:

答案 0 :(得分:1)

在我看来,它应该测试某种工作流程或触发器是否在插入商机记录时在Op_Owner__c字段中设置值。如果测试旨在实际验证应用程序的功能,则调试语句实际上应该是System.assertSystem.assertEquals调用。在测试用例执行期间通常不会查看调试语句。

这是一个清理版本,实际上会对Op_Owner__c字段的值(这是测试用例的目的)进行断言,而不仅仅是将某些内容打印到调试日志中。

Test.startTest();
Opportunity newtestOpp1 = TestUtil.initOpportunity(TestUtil.initAccount(),TestUtil.initContact());
User testUser1 = TestUtil.initUser(); 
newtestOpp1.OwnerId = testUser1.Id;//setting OwnerId
System.assertEquals(null, newtestOpp1.Op_Owner__c, 'The opp owner should be null');
try{        
    insert newtestOpp1;
} catch ( DMLException d ) {
    System.debug(d);
}
System.assertNotEquals(null, newtestOpp1.Op_Owner__c, 'The opp owner should not be null');
相关问题