You need to sign in to do that
Don't have an account?
Chris Stark 3
Writing Tests for ConnectApi.getTopics(...)
Hello,
I am trying to write Unit Tests for the following code snippet:
Any guidance is appreciated, thanks.
I am trying to write Unit Tests for the following code snippet:
// find topics matching the word "Cancer" String query = 'Cancer'; ConnectApi.TopicPage page = ConnectApi.Topics.getTopics(Network.getNetworkId(), query, ConnectApi.topicSort.PopularDesc);This code works fine when actually in use via my application or via Anonymous Apex, but fails to return any results in my unit test:
@isTest(SeeAllData=true) static void testTopicsWithMatches() { ConnectApi.CommunityPage cp = ConnectApi.Communities.getCommunities(); if( cp == null || cp.communities == null || cp.communities.isEmpty() ) { System.assert( false, 'Unable to locate community!' ); } ConnectApi.Community myCommunity = cp.communities.get( 0 ); Test.startTest(); ConnectApi.TopicPage topicPage = ConnectApi.Topics.getTopics( myCommunity.Id, 'Cancer', ConnectApi.topicSort.PopularDesc ); System.debug( topicPage); System.debug( '-----' + topicPage.topics ); Test.stopTest(); }Even with the SeeAllData=true, the returned ConnectApi.TopicPage has no topics. Why is this? Do I need to set mock results for the ConnectApi somehow? If so, using which set method?
Any guidance is appreciated, thanks.
alouie_sfdc
As a first debugging step, try to hard-code your communityId in the call to ConnectApi.Topics.getTopics(). Perhaps the wrong one is getting used. You don't need to set mock results. There isn't a setTest method, and if there was one, you'd get an error when running the test.
Chris Stark 3
Thanks for the response. I updated the test as per your suggestion for debugging, although I believe it was properly picking up the community we want to use:
The debug log from this test execution is as follows:
Unfortunately I do not think that the problem was using the wrong community.