So I've been reading and noticed that as a general rule you should shoot for 75-90% code coverage for a trigger, aiming for 90% or higher if possible.
I feel proud of myself for getting the test coverage to pass. But I cannot for the life of me get it to resolve beyond 16%.
The purpose of the trigger (and it works doing this) is so that when an IT Request lands in the IT Request object, it makes a duplicate of in TaskRay. I also have a trigger that updates the IT Request when it's updated in TaskRay, and perpetuates the data. That one I have working, with 100% code coverage. But I am very lost on why the below won't get above 16%.
trigger VS_ITRequestToTaskRay on IT_Request__c (after insert){
//Needs to be bulkified
List<TASKRAY__Project_Task__c> toCreate = new List<TASKRAY__Project_Task__c>();
TASKRAY__Project__c reqID = [SELECT Id, Name FROM TASKRAY__Project__c WHERE Name = 'IT Requests'];
for(IT_Request__c trt : Trigger.new)
{
TASKRAY__Project_Task__c toTaskRay = new TASKRAY__Project_Task__c();
toTaskRay.IT_Request__c = trt.Id;
toTaskRay.TASKRAY__Description__c = 'Type: ' + trt.Type__c + '. Notes: ' + trt.Remarks__c;
toTaskRay.Name = 'ITREQ - ' + trt.Requestor_Name__c + ' - ' + trt.Subject__c +' - ' + trt.Reason__c;
toTaskRay.TASKRAY__List__c = 'Holding';
toTaskRay.TASKRAY__Project__c = reqID.Id;
toCreate.add(toTaskRay);
}
insert toCreate;
}
I have no idea if that is bulkified correctly or not, but I did try to set it up by adding each loop result to a list, then updating the list. (I've only been learning Apex for about 3 weeks now, and am still a bit confused about bulkify, though it's starting to make sense.)
My test class it this:
@isTest
public class itRequestToTaskRay {
private static void genData()
{
IT_Request__c testRequest = new IT_Request__c();
testRequest.Requestor_Name__c = 'Mister Anderson';
testRequest.Type__c = 'Fix';
testRequest.Subject__c = 'Salesforce';
testRequest.Reason__c = 'Administrator Needed';
testRequest.Remarks__c = 'Wake up, Neo.';
insert testRequest;
}
public static testMethod void testValues()
{
try
{
genData();
IT_Request__c matchAgainst = [SELECT Id, Name, Requestor_Name__c, Remarks__c FROM IT_Request__c WHERE Requestor_Name__c = 'Mister Anderson' LIMIT 1];
TASKRAY__Project_Task__c resultCheck = [SELECT Id, Name, IT_Request__c, TASKRAY__Description__c, TASKRAY__List__c, TASKRAY__Project__c
FROM TASKRAY__Project_Task__c
WHERE IT_Request__c = : matchAgainst.Id LIMIT 1];
Test.startTest();
//find effective way to not repeat self
System.assert(resultCheck.IT_Request__c == matchAgainst.Id);
System.assert(resultCheck.Name != null);
System.assert(resultCheck.TASKRAY__List__c == 'Holding');
System.assert(resultCheck.TASKRAY__Project__c == [SELECT Id, Name FROM TASKRAY__Project__c WHERE Name = 'IT Requests'].Id);
Test.stopTest();
}
catch (DmlException dmle)
{
System.debug(dmle);
}
}
}
Edit: I marked the answer, but also just wanted to post my notes here about what it ultimately was.
1) Remove catch/try as it was preventing me from seeing the errors I need to see. This had to do with data that could not be returned from a SOQL query.
2) The SOQL query returned no data because a test class cannot query data from the live org, or rest of the sandbox. The test class can ONLY see data that is created in the test class. You have to create it all: Contact, Project, User, whatever you need to run your test class.
3) Duplicate's in production won't be an issue within a test class. The test class starts with empty data, and you can populate users with names, or projects with names, that are the same as ones in production/sandbox, and you won't run into any issues so long as the data is created within the test class itself.
try
/catch
block. It's just suppressing errors that you need to see. – Adrian Larson♦ Jun 23 '16 at 21:30assertEquals
andassertNotEquals
. Also, add a second clause with a descriptive error message. The advantages are many, most important that if the assert doesn't work you'll get a much more descriptive error message. As an example, you can doSystem.assertEquals('Holding', resultCheck.TASKRAY__List__c, 'List Values Arent Matching);
– Sebastian Kessel Jun 23 '16 at 21:55