You need to sign in to do that
Don't have an account?

cannot get test past 40% coverage for controller to pull in Dashboard into Visualforce page
public with sharing class DashboardSnippetController
{
public DashBoardSnippetController()
{}
public string getDashboardHtml()
{
PageReference dbPage = new PageReference('https://cs4.salesforce.com/01ZP00000008goB');
Blob pageBlob = dbPage.getContent();
return pageBlob.toString();
}
@isTest
static void testDashboardSnippetController()
{
PageReference pageRef = Page.HomeDS;
Test.setCurrentPage(pageRef);
DashboardSnippetController controller = new DashboardSnippetController();
String nextPage = controller.getDashboardHtml();
// Verify that page fails without parameters
System.assertEquals('/apex/failure?error=noParam', nextPage);
// Add parameters to page URL
ApexPages.currentPage().getParameters().put('pageBlob', '');
// Instantiate a new controller with all parameters in the page
controller = new DashboardSnippetController();
nextPage = controller.getDashboardHtml();
// Verify that the success page displays
System.assertEquals('/apex/HomeDS', nextPage);
}
}
The docs aren't exactly clear about this, but clearly the test runner won't let you use the getContent method.
One way to handle this is to detect that you are in a test and return some fake data, something like:
You won't be able to get coverage for the line that actually gets the content, but hopefully you'll get enough coverage to allow deployment.
All Answers
Can you show us which lines aren't covered? Also, do you get any errors or does the test complete successfully?
Hi Bob - thanks, trying to adapt the code from one of your suggestions
the test completes successfully - this is the only message I get when I use the Apex Test Execution option in SF:
The docs aren't exactly clear about this, but clearly the test runner won't let you use the getContent method.
One way to handle this is to detect that you are in a test and return some fake data, something like:
You won't be able to get coverage for the line that actually gets the content, but hopefully you'll get enough coverage to allow deployment.
You are awesome! thanks - this worked:
public with sharing class DashboardSnippetController
{
public DashBoardSnippetController()
{}
public string getDashboardHtml()
{
PageReference dbPage = new PageReference('https://cs4.salesforce.com/01ZP00000008goB');
Blob pageBlob;
if (Test.isRunningTest())
{
pageBlob=Blob.valueOf('Test Blob String');
}
else { pageBlob = dbPage.getContent();
}
return pageBlob.toString();
}
Glad to hear you got there.