You need to sign in to do that
Don't have an account?
glynn
a newbie testmethod unit test question
I have a testMethod and most of it works fine. However, there is a part of my Apex trigger that my test case is not getting into (executing).
//Logic to handle different email subjects for each FeedItem type if (f.Type == 'ContentPost') { emailSubject = 'New ContentPost'; if (f.ContentData != NULL) { Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setBody(f.ContentData); efa.setFileName(f.ContentFileName); mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); tiBod=tiBod+'\n'+'***See Attached File!'; } else { Map<String,Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); List<Schema.SObjectType> sobjects = schemaMap.values(); List<Sobject> theObjectResults; Schema.DescribeSObjectResult objDescribe; List<Schema.SObjectField> tempFields; for(Schema.SObjectType objType : sobjects) { objDescribe = objType.getDescribe(); String sobjectPrefix = objDescribe.getKeyPrefix(); if(parentid != null && sobjectPrefix != null && parentid.startsWith(sobjectPrefix)) { string objectType = objDescribe.getLocalName(); objectType = objectType + 'Feed'; String qryString = 'SELECT contentData FROM ' + objectType + ' WHERE id' + '=' + '\'' + f.id + '\''; sobject qr = Database.query(qryString); Blob resultObject = (Blob)qr.get('ContentData'); Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setBody(resultObject); efa.setFileName(f.ContentFileName); mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); } } } } else if (f.Type == 'LinkPost') { emailSubject = 'New LinkPost'; tiBod = tiBod + '\n' + 'Link: ' + f.linkUrl; } else { emailSubject = 'New TextPost'; }
How do I test the code within the outside IF and ELSE IF statements. The code that starts at
(f.Type == 'ContentPost')
Once in the IF statement I'm also trying to figure out how to test the MAP and LIST types.
Appreciate any help you can give this newbie. thanks
It would of been nice to see the test code, but from your other code it seems that your IF statement depends on the 'FeedItem' type. I guess this is a custom object.
So in your test code, you could either do two things:
1. Create a 'FeedItem' and set its Type field to 'ContentPost' then update the record and change the type to 'LinkPost'. Finally, change the type to something besides 'ContentPost' and 'LinkPost'.
2. Create three different 'FeedItem' records and set one Type's field to 'ContentPost'. The other to 'LinkPost'. Finally, change the last type to something besides 'ContentPost' and 'LinkPost'.
I usually do the first, because if I have any validation rules on the 'FieldItem' object then I can make sure the test code works on both insert and update operations.
An example of #2 follows:
All Answers
It would of been nice to see the test code, but from your other code it seems that your IF statement depends on the 'FeedItem' type. I guess this is a custom object.
So in your test code, you could either do two things:
1. Create a 'FeedItem' and set its Type field to 'ContentPost' then update the record and change the type to 'LinkPost'. Finally, change the type to something besides 'ContentPost' and 'LinkPost'.
2. Create three different 'FeedItem' records and set one Type's field to 'ContentPost'. The other to 'LinkPost'. Finally, change the last type to something besides 'ContentPost' and 'LinkPost'.
I usually do the first, because if I have any validation rules on the 'FieldItem' object then I can make sure the test code works on both insert and update operations.
An example of #2 follows: