I've been running a chatter tutorial and ran across something I don't understand.
First off here's the link to the tutorial .
The following code is straight out of the tutorial and works, except for the test method. The test method fails on the first assert() every time. The reason it fails is because the array is always empty.
Why is the test method failing but the visualforce page works fine? Better yet, why isn't the getRecentTextUpdates() returning anything in the test method?
<apex:page controller="NewsFeedController" sidebar="false">
<h1>News Feed</h1>
<apex:form >
<apex:inputText value="{!feedUpdate}"/>
<apex:commandButton value="Update" action="{!go}" reRender="recent"/>
<apex:outputPanel id="recent">
<apex:dataList value="{!recentTextUpdates}" var="update">
<apex:outputText value="{!update.body}"></apex:outputText>
</apex:dataList>
</apex:outputPanel>
</apex:form>
</apex:page>
public class NewsFeedController {
@isTest
static void testUpdates() {
NewsFeedController con = new NewsFeedController();
con.feedUpdate = 'Hi';
con.go();
List<NewsFeed> newsFeed = con.getRecentTextUpdates();
System.debug('My NewsFeed Size: ' + newsFeed.size());
System.assert(newsFeed.size() > 0);
System.assertEquals('Hi', newsFeed[0].Body);
}
public PageReference go() {
FeedItem feedItem = new FeedItem();
feedItem.Type = 'TextPost';
feedItem.ParentId = UserInfo.getUserId();
feedItem.Body = this.feedUpdate;
insert feedItem;
return null;
}
public List<NewsFeed> getRecentTextUpdates() {
List<NewsFeed> newsFeed = [SELECT Type, CreatedDate, CreatedBy.Name, Body FROM NewsFeed WHERE Type = 'TextPost' ORDER BY CreatedDate DESC LIMIT 10];
return newsFeed;
}
public String feedUpdate { get; set; }
}