I have a trigger that fires when an Active Date is entered on our custom object Location, the trigger updates the Start Date on all child records (Cans) for that Location, so that all of the records then have the same start date. The trigger works when I test it, and the test class runs and does not give any errors, but I still have 0% code coverage. What am I doing wrong?
Here is the code for the trigger
trigger MassUpdateServicesOnActivation on Location__c (after update)
{
list<Location__c> locations = new list<Location__c>();
//find locations where active date is changed
for(Location__c loc : trigger.new)
{
if(loc.Active_Date__c != trigger.oldMap.get(loc.Id).Active_Date__c)
{
locations.add(loc);
System.debug(loc.Name); //remove
}
}
list<Can__c> cans = new list<Can__C>();
//query services for each location
for(Location__c l : locations)
{
cans = [SELECT Id, Start_Date__c, Status__c FROM Can__c WHERE Locations__c =: l.Id
AND Status__c = 'Proposed'];
//update Start Dates
for(Can__c c : cans)
{
c.Start_Date__c = l.Active_Date__c;
}
}
update cans;
}
And here is the test class
@isTest
public class LocationObjectTest {
public static testMethod void main(){
//create test data
Location__c loc = new Location__c();
//code here add some more required fields to location
insert loc;
//create can
Can__c can = new Can__c();
can.Locations__c = loc.id;
insert can;
//verify that the can and location have been created
System.assertNotEquals(null, loc.Id);
System.assertNotEquals(null, can.Id);
//start test
Test.startTest();
loc.Active_Date__c = date.today();
update loc;
System.assertEquals(date.today(), loc.Active_Date__c);
//check that the can has been updated
list<Can__c> cans = new list<Can__C>();
cans = [SELECT Id, Start_Date__c FROM Can__c WHERE Locations__c =: loc.Id];
//check for Start Dates
for(Can__c c : cans)
{
update c;
System.assertEquals(date.today(), c.Start_Date__c);
System.assertNotEquals(null, c.Start_Date__c);
System.debug(c.Start_Date__c);
}
Test.stopTest();
}
}