Salesforce:从社区获取知识知识趋势文章的APEX测试课程

发布于 2025-02-13 15:16:33 字数 2247 浏览 0 评论 0原文

我需要从社区获取趋势文章。我通过使用 connectapi.knowledge.getTrendingArtsicles(communityId,maxResult)为此创建了一个Apex类。

我需要为此创建一个测试类。我正在使用Salesforce提供的测试类方法。 SettestGetTrendingArtsicles(communityId,MaxResults,结果),但我遇到了此错误” System.System.AsserTexception:断言失败:知识的匹配测试结果。呼叫知识。setTestGetTrendingArtsicles(字符串communityId,Integer MaxResults,ConnectApi.KnowledGeartClecterVersionCollection结果)设置预期的测试结果。”

公众而无需共享类ConnectTopicCatalogController {

 @AuraEnabled(cacheable=true)
public static List<ConnectApi.KnowledgeArticleVersion> getAllTrendingArticles(){
    string commId = [Select Id from Network where Name = 'Customer Community v5'].Id;
    ConnectApi.KnowledgeArticleVersionCollection mtCollection = ConnectApi.Knowledge.getTrendingArticles(commId, 12);
    System.debug('getAllTrendingTopics '+JSON.serializePretty(mtCollection.items));
    List<ConnectApi.KnowledgeArticleVersion> topicList = new List<ConnectApi.KnowledgeArticleVersion>();
    for(ConnectApi.KnowledgeArticleVersion mtopic : mtCollection.items)
    {
            topicList.add(mtopic);
    }
   return topicList;
}

}

我正在使用的测试类

public class ConnectTopicCatalogControllerTest {

public static final string communityId = [Select Id from Network where Name = 'Customer Community v5'].Id;

@isTest
static void getTrendingArticles(){
    ConnectApi.KnowledgeArticleVersionCollection knowledgeResult = new ConnectApi.KnowledgeArticleVersionCollection();

    List<ConnectApi.KnowledgeArticleVersion> know = new List<ConnectApi.KnowledgeArticleVersion>();
    know.add(new ConnectApi.KnowledgeArticleVersion());
    know.add(new ConnectApi.KnowledgeArticleVersion());
    system.debug('know '+know);
    knowledgeResult.items = know;
    
    // Set the test data
    ConnectApi.Knowledge.setTestGetTrendingArticles(null, 12, knowledgeResult);
    List<ConnectApi.KnowledgeArticleVersion> res = ConnectTopicCatalogController.getAllTrendingArticles();
    
    // The method returns the test page, which we know has two items in it.
    Test.startTest();
    System.assertEquals(12, res.size());
    Test.stopTest();
   
}

我需要帮助来解决测试类 谢谢。

I need to get trending articles from the community. I created a apex class for that by using ConnectApi.Knowledge.getTrendingArticles(communityId, maxResult).

I need to create a test class for that. I am using test class method provided by Salesforce for that. setTestGetTrendingArticles(communityId, maxResults, result) but I am getting this error "System.AssertException: Assertion Failed: No matching test result found for Knowledge.getTrendingArticles(String communityId, Integer maxResults). Before calling this, call Knowledge.setTestGetTrendingArticles(String communityId, Integer maxResults, ConnectApi.KnowledgeArticleVersionCollection result) to set the expected test result."

public without sharing class ConnectTopicCatalogController {

 @AuraEnabled(cacheable=true)
public static List<ConnectApi.KnowledgeArticleVersion> getAllTrendingArticles(){
    string commId = [Select Id from Network where Name = 'Customer Community v5'].Id;
    ConnectApi.KnowledgeArticleVersionCollection mtCollection = ConnectApi.Knowledge.getTrendingArticles(commId, 12);
    System.debug('getAllTrendingTopics '+JSON.serializePretty(mtCollection.items));
    List<ConnectApi.KnowledgeArticleVersion> topicList = new List<ConnectApi.KnowledgeArticleVersion>();
    for(ConnectApi.KnowledgeArticleVersion mtopic : mtCollection.items)
    {
            topicList.add(mtopic);
    }
   return topicList;
}

}

Test class that I am using for this

public class ConnectTopicCatalogControllerTest {

public static final string communityId = [Select Id from Network where Name = 'Customer Community v5'].Id;

@isTest
static void getTrendingArticles(){
    ConnectApi.KnowledgeArticleVersionCollection knowledgeResult = new ConnectApi.KnowledgeArticleVersionCollection();

    List<ConnectApi.KnowledgeArticleVersion> know = new List<ConnectApi.KnowledgeArticleVersion>();
    know.add(new ConnectApi.KnowledgeArticleVersion());
    know.add(new ConnectApi.KnowledgeArticleVersion());
    system.debug('know '+know);
    knowledgeResult.items = know;
    
    // Set the test data
    ConnectApi.Knowledge.setTestGetTrendingArticles(null, 12, knowledgeResult);
    List<ConnectApi.KnowledgeArticleVersion> res = ConnectTopicCatalogController.getAllTrendingArticles();
    
    // The method returns the test page, which we know has two items in it.
    Test.startTest();
    System.assertEquals(12, res.size());
    Test.stopTest();
   
}

}

I need help to solve the test class
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮邮递员 2025-02-20 15:16:33

您的控制器希望这些文章在“客户社区V5”社区内部,但是您将社区式参数作为null传递给SettestGetTrendingArtsicles方法。

Your controller expects the articles to be inside the 'Customer Community v5' community, but you are passing the communityId parameter as null to the setTestGetTrendingArticles method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文