+ Start a Discussion
Amit Kumar Singh 19Amit Kumar Singh 19 

Test Class Issue with ConnectApi.Topics.getRecentlyTalkingAboutTopicsForGroup

Dear All,

I have created a customer community where an visualforce page is created to display chatter group funcationality,.
In this page I am displaying Recently Talking about topics using connect API. It works fine, but while writing test class for this I am getting below error message.

System.AssertException: Assertion Failed: No matching test result found for Topics.getRecentlyTalkingAboutTopicsForGroup(String communityId, String groupId). Before calling this, call Topics.setTestGetRecentlyTalkingAboutTopicsForGroup(String communityId, String groupId, ConnectApi.TopicPage result) to set the expected test result.

Here is my code  :-

Apex Class :-
 
public ConnectApi.TopicPage feedItemPage{get;set;}

public CommunityGroupPage()
 {
   // Chatter Group Id
   Id groupid = apexpages.currentpage().getparameters().get('id');

   / pull all chatter groups, faster than individual queries + 1 SOQL
   chatterGroups = [SELECT id, Name, NetworkId FROM CollaborationGroup where id =: groupid Limit 1];
   String comid;
   String grpid;
   if(chatterGroups.Size() > 0)
   {
   comid = chatterGroups[0].NetworkId;
   grpid = chatterGroups[0].Id;   
   }
   
   //This will be used to fetch Recently Talking about topics for current chatter group
   feedItemPage = ConnectApi.Topics.getRecentlyTalkingAboutTopicsForGroup(comId,grpid);
   
 }

Test Class
 
**
 * An apex page controller that exposes the Chatter Group Page functionality
 */
@IsTest global with sharing class CommunityGroupPageTest {
    @IsTest(SeeAllData=true) global static void testCommunityGroupPage () {
        // Instantiate a new controller with all parameters in the page

   Network community;
   String communityId;

   Id currentUserId = UserInfo.getUserId();    
        User usr = [select id,Name from User where id =: currentUserId];
        
        System.runAs(usr) {
        community = [SELECT id, Name,OptionsReputationEnabled FROM Network where name =: 'Customer Community'];
        communityId = community.id;
        
        collaborationgroup cg = new collaborationgroup(name ='ChatterGroupPage',CollaborationType='Public',OwnerId=currentUserId,NetworkId=communityId);
        insert cg;
        
       PageReference pageRef = Page.CommunityChatterPage;
        Test.setCurrentPage(pageRef);//Applying page context here
        // Add parameters to page URL
        ApexPages.currentPage().getParameters().put('id', cg.id);//Observe how helpful it was to set the parameters in your page from Unit test 
        
        CommunityGroupPage controller = new  CommunityGroupPage ();
        
        controller.groupid = System.currentPagereference().getParameters().get('id');

        ConnectApi.TopicPage testPage = new ConnectApi.TopicPage();
        List<ConnectApi.Topic> testItemList = new List<ConnectApi.Topic>();
        testItemList.add(new ConnectApi.Topic());
        testItemList.add(new ConnectApi.Topic());
                
        testPage.topics = testItemList;

        ConnectApi.Topics.setTestGetRecentlyTalkingAboutTopicsForGroup(communityId, cg.id, testPage);
        ConnectApi.Topics.getRecentlyTalkingAboutTopicsForGroup(communityId,cg.id); 

        }
}
}

Please help me to fix this issue.

Thanks,
AMit Singh
Best Answer chosen by Amit Kumar Singh 19
alouie_sfdcalouie_sfdc
You need to register the test stub by calling ConnectApi.Topics.setTestGetRecentlyTalkingAboutTopicsForGroup(communityId, cg.id, testPage) before you create the CommunityGroupPage controller, because it's the CommunityGroupPage constructor that calls ConnectApi.Topics.getRecentlyTalkingAboutTopicsForGroup(comId,grpid).

Also, line 38 in your test should be deleted. You're already calling that method when you call the CommunityGroupPage constructor.

All Answers

alouie_sfdcalouie_sfdc
You need to register the test stub by calling ConnectApi.Topics.setTestGetRecentlyTalkingAboutTopicsForGroup(communityId, cg.id, testPage) before you create the CommunityGroupPage controller, because it's the CommunityGroupPage constructor that calls ConnectApi.Topics.getRecentlyTalkingAboutTopicsForGroup(comId,grpid).

Also, line 38 in your test should be deleted. You're already calling that method when you call the CommunityGroupPage constructor.
This was selected as the best answer
Amit Kumar Singh 19Amit Kumar Singh 19
Thanks a lot alouie_sfdc , Now issue is resolved. :)