You need to sign in to do that
Don't have an account?
Chelsea Lukowski
Copy attachment test class assertion failed error
I have a trigger to copy attachments that are attached by a certain profile from a customer object Product_Engineering_Memo__c to the related Case. I need help with the test class. I keep getting an error System.AssertException: Assertion Failed: Expected: 1, Actual: 0
Here is my trigger:
My test class:
Here is my trigger:
trigger CopyProductEngineeringAttchmentToCase on Attachment (before insert) { // collect a set of Sales Order 'parent' IDs from the attachments inserted Set<Id> engMemoIds = new Set<Id>(); Id profileId=userinfo.getProfileId(); String profileName=[Select Id,Name from Profile where Id=:profileId].Name; for(Attachment file : Trigger.new) { // only collect those that are for the Product_Engineering_Memo__c object (others can be ignored) if(file.ParentId.getSObjectType() == Product_Engineering_Memo__c.getSObjectType() && ('Titan Pricing Analyst'.Equals(profileName))){ engMemoIds.add(file.ParentId); } } if(!engMemoIds.isEmpty()) { // find the Opportunity to which the Service_Order__c relates Map<Id,Product_Engineering_Memo__c> memoMap = new Map<Id,Product_Engineering_Memo__c>([Select New_Wheel_request__c From Product_Engineering_Memo__c Where Id IN :engMemoIds]); List<Attachment> attachments = new List<Attachment>(); for(Attachment file : Trigger.new) { Attachment newFile = file.clone(); newFile.ParentId = memoMap.get(file.ParentId).New_Wheel_request__c; attachments.add(newFile); } // finally, insert the cloned attachments insert attachments; } }
My test class:
@isTest public class CopyProductEngineeringAttachToCaseClass { static testMethod void CopyAttachToCaseClass() { String userID = '005o0000001c1vd'; // -- Create and insert Account instance Account acc = new Account(Name = 'Test Account',Phone = '217-222-1111',Ship_Via__c = 'PPD', Channel_of_Distribution__c = 'Aftermarket',Market_Code__c = 'Aftermarket',Market_Segment__c = 'Aftermarket'); insert acc; //create and insert Contact Contact con = new Contact(AccountId = acc.Id,LastName = 'TestLastName',FirstName = 'TestFristName'); insert con; // -- Create and insert Memo instance Product_Engineering_Memo__c memo1 = new Product_Engineering_Memo__c(); memo1.date__c = date.TODAY(); memo1.Customer_Name_LOC__c = acc.id; memo1.Customer_Contact__c = con.id; memo1.Item__c = 'Testing this trigger'; memo1.Customer_Prints__c = 'TestPrints'; memo1.Use_Application__c = 'TestApplication'; memo1.Annual_Usage__c = 'TestUsage'; memo1.Paint_Codes__c = 'OTHER'; memo1.Stage__c = 'Quote'; insert memo1; Case c = new case(Status = 'New',Memo__c = memo1.Name); insert c; // -- Create and insert Attachment instances and relate to Memo Attachment att1 = new Attachment(); att1.Name = 'Unit Test Attachment 1'; att1.body = Blob.valueOf('Unit Test Attachment Body 1'); att1.parentId = memo1.id; insert att1; // -- Retrieve saved attachments List<Attachment> attachments = [select id, name, body from Attachment where parent.id=:c.id]; System.assertEquals(1, attachments.size()); } }
1) RunAs User you need to add for "Titan Pricing Analyst" profile
2) You need to create the New_Wheel_request__c object and need to set on New_Wheel_request__c field on Product_Engineering_Memo__c object
// -- Create and insert Memo instance
Product_Engineering_Memo__c memo1 = new Product_Engineering_Memo__c();
memo1.date__c = date.TODAY();
memo1.Customer_Name_LOC__c = acc.id;
memo1.Customer_Contact__c = con.id;
memo1.Item__c = 'Testing this trigger';
memo1.Customer_Prints__c = 'TestPrints';
memo1.Use_Application__c = 'TestApplication';
memo1.Annual_Usage__c = 'TestUsage';
memo1.Paint_Codes__c = 'OTHER';
memo1.Stage__c = 'Quote';
memo1.New_Wheel_request__c =''; // Please add this value here
insert memo1;
Then try below test class
Let us know if that will help you
All Answers
Try to update your test class like below
Let us know if that will work for you
1) RunAs User you need to add for "Titan Pricing Analyst" profile
2) You need to create the New_Wheel_request__c object and need to set on New_Wheel_request__c field on Product_Engineering_Memo__c object
// -- Create and insert Memo instance
Product_Engineering_Memo__c memo1 = new Product_Engineering_Memo__c();
memo1.date__c = date.TODAY();
memo1.Customer_Name_LOC__c = acc.id;
memo1.Customer_Contact__c = con.id;
memo1.Item__c = 'Testing this trigger';
memo1.Customer_Prints__c = 'TestPrints';
memo1.Use_Application__c = 'TestApplication';
memo1.Annual_Usage__c = 'TestUsage';
memo1.Paint_Codes__c = 'OTHER';
memo1.Stage__c = 'Quote';
memo1.New_Wheel_request__c =''; // Please add this value here
insert memo1;
Then try below test class
Let us know if that will help you