0

I'm trying to write a unit test for my trigger, which operates on After Insert, After Update, After Delete. The trigger works great in real life, but I can't get the unit test to do what I want.

The trigger is designed to count all of the child records and give them a sequence number (in the field Activity_Sequence__c) based on their Scheduled_Date__c field. So, for example, if I insert 20 records, the sequence will be (1 of 20), (2 of 20), (3 of 20), etc...

In my test class, I start by inserting 20 records, and then checking that the sequence worked properly. It does. After that, I try to delete 5 of the records. After I delete the 5 records, the Activity Sequence field should be (1 of 15), (2 of 15), etc..., but is actually still (1 of 20), (2 of 20), etc.. It doesn't update after delete, and I don't understand why.

@isTest

private class ActivitySequenceTest {

public static testMethod void ActivitySequenceTest() {

    //Date
    DateTime schedule = System.now();

    //User
    Profile p1 = [SELECT Id FROM Profile WHERE Name='System Administrator'];

    User u1 = new User(Alias = 'Holly', Email='holly@testorg.com', 
    EmailEncodingKey='UTF-8', LastName='Molly', LanguageLocaleKey='en_US', 
    LocaleSidKey='en_US', ProfileId = p1.Id, 
    TimeZoneSidKey='America/Los_Angeles', UserName='Holly#4@testorg.com');
    insert u1;

    //Create a Study Object
    Study__c cStudy = new Study__c();
    cStudy.Name = 'TestStudy';
    insert cStudy;

    //Create a Vendor Object
    Vendor__c vendor = new Vendor__c();
    vendor.Name = 'TestVendor';
    insert vendor;

    List<Activity__c> insertActivities = new List<Activity__c>();

    for(Integer i = 1; i < 21; i++)
    {
        Activity__c v = new Activity__c(Activity__c='Some Text Here',Vendor__c=vendor.Id,Study__c=cStudy.Id, Assigned_to__c=u1.Id, Status__c='Scheduled', Scheduled_Date__c = schedule.date()+i);
        insertActivities.add(v);
    }

    Integer s = insertActivities.size();

    Test.startTest();

    //***TEST 1: INSERT ACTIVITIES***
    insert insertActivities;

    List<Activity__c> assertInsertActivities = [SELECT Id, Activity_Sequence__c, Activity__c, Study__c, Scheduled_Date__c 
                                                    FROM Activity__c 
                                                    WHERE Study__c = :cStudy.Id 
                                                    ORDER BY Scheduled_Date__c ASC];

    //test that inserted activities were correctly sequenced
    for(Integer j = 1; j < s; j++)
    {  
       System.assertEquals('('+j+' of '+s+')', assertInsertActivities[j-1].Activity_Sequence__c);    
    }

    //***TEST 2: DELETE ACTIVITIES***
    List<Activity__c> deleteActivities = [SELECT Id, Activity_Sequence__c, Activity__c, Study__c, Scheduled_Date__c 
                                                                FROM Activity__c 
                                                                WHERE Study__c = :cStudy.Id 
                                                                ORDER BY Scheduled_Date__c DESC
                                                                LIMIT 5];
    delete deleteActivities;

    List<Activity__c> assertDeletedActivities = [SELECT Id, Study__c, Activity__c, Activity_Sequence__c
                                                            FROM Activity__c
                                                            WHERE Study__c = :cStudy.Id];

    System.debug(assertDeletedActivities.size());

    for(Activity__c x: assertDeletedActivities)
    {
       System.debug(x.Activity_Sequence__c);
    }

    Test.stopTest();

    }
}
2

For starters, you should make each case a separate testMethod. You can avoid some copy-pasta by abstracting some common setup out into a helper method.

static My_Object__c setupRecord()
{
    // common setup
    // do not insert
}

static testMethod void testAfterInsert()
{
    My_Object__c record = setupRecord();
    Test.startTest();
        insert record;
    Test.stopTest();
    // assert stuff
}
static testMethod void testAfterUpdate()
{
    My_Object__c record = setupRecord();
    insert record;
    Test.startTest();
        update record;
    Test.stopTest();
    // assert stuff
}
static testMethod void testAfterDelete()
{
    My_Object__c record = setupRecord();
    insert record;
    Test.startTest();
        delete record;
    Test.stopTest();
    // assert stuff
}
  • Thank you Adrian, I'm rather new at this and your explanation helps. – rwegner7 Jan 28 '15 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.