如何在 Salesforce 中获得代码覆盖率?
我不是开发人员,但在 GitHub 上找到了一些信息,其中提供了一个 Apex 类,该类在创建报价时创建报价 PDF。我在沙箱中进行了测试,一切正常,但是它告诉我我的代码覆盖率为 0,因此我无法部署到 Prod。以下是 Apex 及其测试。对可能出什么问题有什么想法吗?我按照此文章提供了代码的链接。
Apex 类
`公共类generateQuotePdfDocument{
@InvocableMethod
public static void CreateQuote(List<Id> quoteIds)
{
createQuoteFutureMethod(quoteIds);
}
@future(callout=true)
public static void createQuoteFutureMethod (List<Id> quoteIds) {
//Initialize the quote url
String quoteUrl = '/quote/quoteTemplateDataViewer.apexp?';
//Get the Quote Template Id from Custom Settings
String quoteTemplateId = Label.QuoteTemplateId;
//List variable to get all the Quote Documents
List<QuoteDocument> lstQuoteDoc = new List<QuoteDocument>();
if(!quoteIds.isEmpty() && quoteIds.size() > 0) {
for(Id quoteId :quoteIds) {
//Construct the quote URL to generate PDF
quoteUrl += 'id=' + quoteId;
quoteUrl += '&headerHeight=197&footerHeight=10';
quoteUrl += '&summlid=' + quoteTemplateId;
//call the quote url
PageReference pageRef = new PageReference(quoteUrl);
//get the quotePdf
Blob quoteBlob;
if(Test.isRunningTest()) {
quoteBlob = Blob.valueOf('Generate Pdf');
} else {
quoteBlob = pageRef.getContentAsPDF();
}
//initialze the QuoteDocument to hold the quote pdf for insertion
QuoteDocument quoteDoc = new QuoteDocument();
quoteDoc.Document = quoteBlob;
quoteDoc.QuoteId = quoteId;
lstQuoteDoc.add(quoteDoc);
}
}
if(!lstQuoteDoc.isEmpty() && lstQuoteDoc.size() > 0) {
Database.insert(lstQuoteDoc);
}
}
}`
测试类
`@istest
私有类generateQuotePdfDocumentTest {
@testSetup
static void setup() {
Product2 产品 = new Product2();
product.Name = '测试产品';
产品.ProductCode = '123';
product.IsActive = true;
插入产品;
PricebookEntry pbe = new PricebookEntry();
pbe.Pricebook2Id = Test.getStandardPricebookId();
pbe.Product2Id = 产品.Id;
pbe.IsActive = true;
pbe.UnitPrice = 10;
insert pbe;
Opportunity op = new Opportunity();
op.Name = '测试';
op.Type = '价值主张';
op.Amount=1200;
op.CloseDate = Date.today().addDays(2);
op.StageName = 'Created';
insert op;
Quote quote = new Quote();
quote.OpportunityId = op.Id;
quote.Name = 'TestQuote';
quote.ExpirationDate = Date.today().addDays(5);
quote.Status = '草稿';
quote.Pricebook2Id = Test.getStandardPricebookId();
插入报价;
QuoteLineItem qli = new QuoteLineItem();
qli.QuoteId = quote.Id;
qli.Quantity = 2;
qli.PricebookEntryId = pbe.Id;
qli.UnitPrice = 20;
插入qli;
}
@isTest
static voidgenerateQuotePdfTest() {
Quote quote = [SELECT Id, Status FROM Quote LIMIT 1];
quote.Status = 'Draft';
更新报价;
}
}`
在尝试部署到生产环境时,我意识到我的代码覆盖率为 0。因此,我正在尝试学习如何纠正此问题。
I'm not a developer but found some info on GitHub which gave an Apex class that creates a Quote PDF upon creating the Quote. I tested in my sandbox and everything works perfectly, however it's telling me I have 0 code coverage so I can't deploy to Prod. Below are is the Apex and the Test for it. Any thoughts on what could be wrong? I followed all the steps in this article which provided the link to the code.
Apex Class
`Public class generateQuotePdfDocument{
@InvocableMethod
public static void CreateQuote(List<Id> quoteIds)
{
createQuoteFutureMethod(quoteIds);
}
@future(callout=true)
public static void createQuoteFutureMethod (List<Id> quoteIds) {
//Initialize the quote url
String quoteUrl = '/quote/quoteTemplateDataViewer.apexp?';
//Get the Quote Template Id from Custom Settings
String quoteTemplateId = Label.QuoteTemplateId;
//List variable to get all the Quote Documents
List<QuoteDocument> lstQuoteDoc = new List<QuoteDocument>();
if(!quoteIds.isEmpty() && quoteIds.size() > 0) {
for(Id quoteId :quoteIds) {
//Construct the quote URL to generate PDF
quoteUrl += 'id=' + quoteId;
quoteUrl += '&headerHeight=197&footerHeight=10';
quoteUrl += '&summlid=' + quoteTemplateId;
//call the quote url
PageReference pageRef = new PageReference(quoteUrl);
//get the quotePdf
Blob quoteBlob;
if(Test.isRunningTest()) {
quoteBlob = Blob.valueOf('Generate Pdf');
} else {
quoteBlob = pageRef.getContentAsPDF();
}
//initialze the QuoteDocument to hold the quote pdf for insertion
QuoteDocument quoteDoc = new QuoteDocument();
quoteDoc.Document = quoteBlob;
quoteDoc.QuoteId = quoteId;
lstQuoteDoc.add(quoteDoc);
}
}
if(!lstQuoteDoc.isEmpty() && lstQuoteDoc.size() > 0) {
Database.insert(lstQuoteDoc);
}
}
}`
Test Class
`@istest
private class generateQuotePdfDocumentTest {
@testSetup
static void setup() {
Product2 product = new Product2();
product.Name = 'Test Product ';
product.ProductCode = '123';
product.IsActive = true;
insert product;
PricebookEntry pbe = new PricebookEntry();
pbe.Pricebook2Id = Test.getStandardPricebookId();
pbe.Product2Id = product.Id;
pbe.IsActive = true;
pbe.UnitPrice = 10;
insert pbe;
Opportunity op = new Opportunity();
op.Name = 'Test';
op.Type = 'Value Proposition';
op.Amount= 1200;
op.CloseDate = Date.today().addDays(2);
op.StageName = 'Created';
insert op;
Quote quote = new Quote();
quote.OpportunityId = op.Id;
quote.Name = 'TestQuote';
quote.ExpirationDate = Date.today().addDays(5);
quote.Status = 'Draft';
quote.Pricebook2Id = Test.getStandardPricebookId();
insert quote;
QuoteLineItem qli = new QuoteLineItem();
qli.QuoteId = quote.Id;
qli.Quantity = 2;
qli.PricebookEntryId = pbe.Id;
qli.UnitPrice = 20;
insert qli;
}
@isTest
static void generateQuotePdfTest() {
Quote quote = [SELECT Id, Status FROM Quote LIMIT 1];
quote.Status = 'Draft';
update quote;
}
}`
In my attempt to deploy to production I realized my code coverage was 0. So I'm trying to learn how to correct this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有在测试方法中调用代码。
对不起,我的手机,但类似
You are not calling your code in your test method.
Sorry on my phone but something like
您如何在班级中称这种不可行的方法为“ createequote”?我猜您在测试课程中进行的更新不是启动可不可行的方法,因此0覆盖范围。尝试进行更新,以使该条件在测试类中匹配以触发可视化方法。
How are you calling this invocable method in your class "CreateQuote"? I guess the the update you are doing in your test class is not firing that invocable method hence the 0 coverage. Try making the update so that the condition matches in your test class for firing the invocable method.