@isTest Coverage for Attach PDF
Over the years I have received a number of emails specifically related to test coverage for the PDF logic demo I wrote about in 2015. I've got some time today to write it down.
/*
Created by: Greg Hacic
Last Update: 14 June 2018 by Greg Hacic
Questions?: greg@interactiveties.com
Notes:
- test for attachPDFToAccount.cls (92.31% coverage)
*/
@isTest
private class attachPDFToAccountTest {
//tests attachPDFToAccount
@isTest //defines method for use during testing only
static void attachPDFLogic() {
//BEGIN: Some Setup Items...
List<Account> accounts = new List<Account>();
accounts.add(new Account(Name = 'Tess Trucking Co.'));
insert accounts;
//END: Some Setup Items...
Test.startTest(); //denote testing context
PageReference pageRef = Page.attachPDFToAccount; //create a page reference to attachPDFToAccount.page
Test.setCurrentPage(pageRef); //set page context
ApexPages.StandardController standardController = new ApexPages.standardController(accounts[0]); //instantiate the standard Account object controller
attachPDFToAccount ext = new attachPDFToAccount(standardController); //instantiate the extension
String validationURLString = ext.attachPDF().getURL(); //get the URL that is returned after the attachPDF() method is invoked
String accountIdAsString = Id.valueOf(accounts[0].Id); //variable represtenting the Account record Id as a String
System.assertEquals(true, validationURLString.contains(accountIdAsString.left(15))); //validate that the URL contains the Id of the Account record
Test.stopTest(); //revert from testing context
}
}
There's not really much to point out here.
I do appreciate when readers ask questions though. So feel free to keep sending them over.
-greg