You need to sign in to do that
Don't have an account?
Stuck on writing testMethod for "saveAttachment" action button
Here's the Apex code I'm writing a testMethod for:
public class RequestFacultyApproval {
:
:
:
public Attachment attachment; // For uploading attachments to Student obj
:
:
:
public PageReference saveAttachment() { // Add the attachment to our Person-Account(Student) record attachment.parentid = StudentDetails.id; insert attachment; // Insert Attachment to both Faculty_Approval__c objects, so it is viewable to Faculty // Members when they access approval form via Customer Portal fac1Attachment = new Attachment(); fac1Attachment.parentid = System.currentPageReference().getParameters().get('fa1'); fac1Attachment.Body = attachment.Body; fac1Attachment.Name = attachment.Name; fac1Attachment.ContentType = attachment.ContentType; insert Fac1Attachment; fac2Attachment = new Attachment(); fac2Attachment.parentid = System.currentPageReference().getParameters().get('fa2'); fac2Attachment.Body = attachment.Body; fac2Attachment.Name = attachment.Name; fac2Attachment.ContentType = attachment.ContentType; insert Fac2Attachment; // redraw the page, so user can see their Attachment has been added to Student record PageReference page = ApexPages.currentPage(); page.setRedirect(true); return page; }
public static testMethod void FacultyApprovalWizard() {
:
:
:
// Create an attachment, test the SaveAttachment button
Attachment attachment = new Attachment();
attachment.Name = 'testAttach';
attachment.Body = Blob.valueOf( 'this is an attachment test' );
string pgSaveAttach = rfa.saveAttachment().getUrl();
}
}
During run test, the highlighted section throws the following DML Exception error:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, Body]: [Name, Body]
I think I understand why -- the Attachment I'm creating in my testMethod doesn't have any context in the saveAttachment action -- but I'm not sure how to make that happen.
public Attachment attachment { get;set; }
Would that help?