You need to sign in to do that
Don't have an account?
Apex developer 21
testclass safe and attach pdf
I have the following controller extetion which works to make a pdf as attachment, but need some help writing a testclass for so far i have the following:
I get the error invalid field initializer
public with sharing class attachPDF { private final Facturatie__c a; public attachPDF(ApexPages.StandardController standardPageController) { a = (Facturatie__c)standardPageController.getRecord(); //instantiate the Facturatie__c object for the current record } Facturatie__c currentRecord = [SELECT Id, Accountname__r.Name FROM Facturatie__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')]; public PageReference attachPDF() { PageReference pdfPage = Page.Factuur2PDF; pdfPage.getParameters().put('id',a.id); Blob pdfBlob = pdfPage.getContent(); Attachment attach = new Attachment(parentId = a.Id, Name = 'Factuur ' + '-' + currentRecord.Accountname__r.Name +'-'+ date.today().format() +'.pdf', body = pdfBlob); //create the attachment object insert attach; PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view(); pageWhereWeWantToGo.setRedirect(true); return pageWhereWeWantToGo; } }
@isTest(seeAllData=true) public class attachPDFTestClass { static testMethod void testAttachments() { Facturatie__c a = new Facturatie__c(Facturatie__c.Accountname__r.Name='Test'); insert a; Attachment attach=new Attachment(); attach.Name='Unit Test Attachment'; Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); attach.body=bodyBlob; attach.parentId=a.id; insert attach; List<Attachment> attachments=[select id, name from Attachment where parent.id=:a.id]; System.assertEquals(1, attachments.size()); Test.StartTest(); FeedItem f = new FeedItem(); f.ParentId = a.id; f.body = 'test'; insert f; FeedComment fc = new FeedComment(); fc.CommentBody = 'legal test'; fc.FeedItemId = f.Id; insert fc; Test.StopTest(); System.assertEquals ('legal test', fc.commentbody); } }
I get the error invalid field initializer
you need to instantiate your controller extension. For example, name of your extension class is "attachPDF", I dont see that you are calling it any where in the test class. You will never get a code coverage until you do that... try something like below and see last two lines,
All Answers
Could you show this by example?? I dont get the 'in line 8'
Try adding below to your test class,
you need to instantiate your controller extension. For example, name of your extension class is "attachPDF", I dont see that you are calling it any where in the test class. You will never get a code coverage until you do that... try something like below and see last two lines,