Salesforce:用于从社区获取热门知识文章的Apex测试类



我需要从社区获得热门文章。我使用ConnectApi.Knowledge创建了一个顶点类。getTrendingArticles (communityId maxResult)。

我需要为此创建一个测试类。我使用的是Salesforce提供的测试类方法。setTestGetTrendingArticles(communityId, maxResults, result)但我得到这个错误"系统。AssertException: Assertion Failed:没有找到与知识匹配的测试结果。getrendingarticles (String communityId, Integer maxResults)。在调用这个之前,请调用知识。settestgetrendingarticles (String communityId, Integer maxResults, ConnectApi。)KnowledgeArticleVersionCollection result)设置预期的测试结果。"

公共不共享类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();

}

}

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

控制器期望文章位于'Customer Community v5'社区内,但是您将communityId参数作为空值传递给setTestGetTrendingArticles方法。

最新更新