function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chelsea LukowskiChelsea 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:
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());            
        }   
}

 
Best Answer chosen by Chelsea Lukowski
Amit Chaudhary 8Amit Chaudhary 8
below two changes you need to do in your test class
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
@isTest
public class CopyProductEngineeringAttachToCaseClass {

    
    static testMethod void CopyAttachToCaseClass() 
	{
                
        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';
		
		memo1.New_Wheel_request__c =''; // Please add this value here
		
        insert memo1;
        
        Case c = new case(Status = 'New',Memo__c = memo1.Name);
        insert c;

		Profile p = [SELECT Id FROM Profile WHERE Name='Titan Pricing Analyst']; 
		
		User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
							EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
							LocaleSidKey='en_US', ProfileId = p.Id, 
							TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com'
		);

		insert u;
		
		System.runAs(u) 
		{
			// -- 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 parentid=:memo1.id];
			System.assertEquals(1, attachments.size());
		}
		
    }
}


Let us know if that will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Change your query like below
List<Attachment> attachments = [select id, name, body from Attachment where parentid=:memo1.id];

Try to update your test class like below
@isTest
public class CopyProductEngineeringAttachToCaseClass {

    
    static testMethod void CopyAttachToCaseClass() 
	{
                
        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 parentid=:memo1.id];
        System.assertEquals(1, attachments.size());
    }
}

Let us know if that will work for you
Chelsea LukowskiChelsea Lukowski
Thank you! That got rid of the error, but it is only covering 42% of the code. It is not testing that the attachment was copied to the case. 
Amit Chaudhary 8Amit Chaudhary 8
below two changes you need to do in your test class
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
@isTest
public class CopyProductEngineeringAttachToCaseClass {

    
    static testMethod void CopyAttachToCaseClass() 
	{
                
        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';
		
		memo1.New_Wheel_request__c =''; // Please add this value here
		
        insert memo1;
        
        Case c = new case(Status = 'New',Memo__c = memo1.Name);
        insert c;

		Profile p = [SELECT Id FROM Profile WHERE Name='Titan Pricing Analyst']; 
		
		User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
							EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
							LocaleSidKey='en_US', ProfileId = p.Id, 
							TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com'
		);

		insert u;
		
		System.runAs(u) 
		{
			// -- 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 parentid=:memo1.id];
			System.assertEquals(1, attachments.size());
		}
		
    }
}


Let us know if that will help you
 
This was selected as the best answer
Chelsea LukowskiChelsea Lukowski
That did it. I have 100% code coverage. Thank you for the help!