1.测试Apex触发器
在部署触发器之前,编写单元测试以执行触发触发器的操作并验证预期结果。
让我们测试一下我们在编写Apex触发器单元之前使用过的触发器。 如果帐户记录具有相关机会,AccountDelete触发器将阻止删除该记录。
trigger AccountDeletion on Account (before delete) {
// Prevent the deletion of accounts if they have related opportunities. for (Account a : [SELECT Id FROM Account WHERE Id IN (SELECT AccountId FROM Opportunity) AND Id IN :Trigger.old]) {
Trigger.oldMap.get(a.Id).addError( 'Cannot delete account with related opportunities.'); } }
测试代码:
@isTest private class TestAccountDeletion {
@isTest static void TestDeleteAccountWithOneOpportunity() {
// Test data setup // Create an account with an opportunity, and then try to delete it Account acct &#
|