gpt4 book ai didi

testing - APEX 触发器的 Salesforce 测试类

转载 作者:行者123 更新时间:2023-11-28 20:56:09 26 4
gpt4 key购买 nike

我需要一些关于编写测试脚本的帮助,该脚本涵盖了我已经设法在我的 Sandbox 帐户上运行的足够多的以下触发器。触发器是在关闭某些类型的机会时创建额外 Assets 。触发器似乎运行良好,但我真的不知道如何开始编写测试用例......为了关闭这些机会,帐户需要完成以下操作(我已经包含了一些示例数据 - 它们是选择列表所以需要具体金额):

a.TurnoverBand__c = '<£10 million';
a.Joining_Fee__c = '£1,920';
a.Annual_Subscription__c = '£1,320';

触发如下:

trigger CreateInclusiveAssetonMembershipWon on Opportunity (after insert, after update)
{
for(Opportunity o: trigger.new)
{
if(o.isWon == true && o.HasOpportunityLineItem == true && ( o.Type == 'A Membership' || o.Type == 'AB Membership' || o.Type == 'A Membership Upgrade' || o.Type == 'AB Membership Upgrade' ) )
{
String opptyId = o.Id;
Asset[] ast = new Asset[]{};
Asset a = new Asset();
{
a = new Asset();
a.AccountId = o.AccountId;
a.Product2Id = '01tA0000003N1pW';
a.Quantity = o.Inclusive_Training_Spaces_Allocated__c;
a.Price = 300;
a.PurchaseDate = o.CloseDate;
a.Status = 'Purchased';
a.Description = 'Allocated Spaces';
a.Name = 'Membership Inclusive Training';
ast.add(a);
}
insert ast;
}
}
}

如果有人能帮我解决这个问题,我将不胜感激!

谢谢

目前此触发器的 ETA 测试脚本:

@isTest
private class TrngAstOppTrigTestSuite {

static testMethod void verifyBehaviorOnInsert_positive() {
Account a = new Account();
a.Name = 'New Test Account';
a.Account_Email__c = 'testemail4trigger@test.co.uk';
a.TurnoverBand__c = '<£10 million';
a.Joining_Fee__c = '£1,920';
a.Annual_Subscription__c = '£1,320';
insert a;

Opportunity o = new Opportunity();
OpportunityLineItem ol = new OpportunityLineItem();
PricebookEntry pbID = [select ID from PricebookEntry];

o.AccountId = a.Id;
o.Name = 'test';
o.Type = 'A Membership';
o.StageName = 'Needs Analysis';
o.CloseDate = date.today();
insert o;

ol.OpportunityId = o.Id;
ol.Quantity = 1;
ol.UnitPrice = 2.00;
ol.PricebookEntryId = pbID.Id;

insert ol;

o.StageName= 'Closed Won';
update o;

delete ol;
delete o;
}
}

如果有人能说我是否朝着正确的方向前进,我将不胜感激。试图消除错误,但如果这无论如何都行不通,显然没有意义。谢谢

最佳答案

Here is a link to the Apex code documentation that shows how to create a test.

您需要做的就是编写一个 testMethod 来插入或更新机会,同时满足您在触发器中定义的条件。一个好的单元测试应该测试各种场景并验证代码是否产生了预期的输出(在这种情况下,查询新 Assets )。

此外,我应该指出您的代码在设计上存在严重缺陷。您几乎不应该在循环中包含 DML 语句(或与此相关的任何数据库语句)。我已经为您提供了代码的固定版本,但我强烈建议您转到 developer.force.com并遵循一些入门 Material 以避免将来出现问题。

trigger CreateInclusiveAssetonMembershipWon on Opportunity (after insert, after update)
{
Asset[] assets = new Asset[0];
for(Opportunity o: trigger.new)
{
if(o.isWon == true && o.HasOpportunityLineItem == true && ( o.Type == 'A Membership' || o.Type == 'AB Membership' || o.Type == 'A Membership Upgrade' || o.Type == 'AB Membership Upgrade' ) )
{

Asset a = new Asset();
a.AccountId = o.AccountId;
a.Product2Id = '01tA0000003N1pW';
a.Quantity = o.Inclusive_Training_Spaces_Allocated__c;
a.Price = 300;
a.PurchaseDate = o.CloseDate;
a.Status = 'Purchased';
a.Description = 'Allocated Spaces';
a.Name = 'Membership Inclusive Training';
assets.add(a);
}
}
insert assets;
}

关于testing - APEX 触发器的 Salesforce 测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17522626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com