0

I'm currently trying to test my trigger but it keeps expecting null instead of date.today(). Can anyone help me?

Trigger:

Trigger UpdateLastSurveySent on Case (after insert, after update) {
    List<Account> accList = [SELECT Id FROM Account WHERE Last_Survey_Sent__c < LAST_N_DAYS:30 or Last_Survey_Sent__c = null];
    for (Case c : Trigger.new) {
        if (c.Status == 'Closed' || c.Status == 'Closed - Pending Approval') {
            c.Last_Survey_Sent__c = date.today();
            Account acc = new Account(
                Id = c.Account.Id,
                Last_Survey_Sent__c = c.Last_Survey_Sent__c
            );
            accList.add(acc);
        }
    }
    try {
        update accList;
    } catch (Exception ex) {
        System.debug('Update failed on Last Survey Sent field on Account with cause: ' + ex.getCause());
    }
}

Test class:

@IsTest    
public class UpdateSurvey {
    @isTest static void testLastSurveySent() {

        Account acc = new Account(
           Name = 'Test Account',
           Website = 'www.test.com',
           Phone = '8888888888'
        );
        insert acc;
        Case c = new Case(
            AccountId = acc.Id,
            Last_Survey_Sent__c = Date.Today()
        );
        insert c;

        // Test on account for insert
        Account accTest = [SELECT Id, Last_Survey_Sent__c FROM Account WHERE Id = :acc.id];
        System.assertEquals(accTest.Last_Survey_Sent__c, Date.Today());

        // Test on account for update
        c.Last_Survey_Sent__c = Date.Today()+1;
        update c;
        accTest = [SELECT Id, Last_Survey_Sent__c FROM Account WHERE Id = :acc.id];
        System.assertEquals(accTest.Last_Survey_Sent__c, Date.Today());
    }
}
  • 1
    Wheres your test class? – battery.cord Oct 23 '14 at 20:03
  • I have edited my question. Forgot to add it in. – Brian Nguyen Oct 23 '14 at 20:07
  • 2
    Your test Case needs the Status set to 'Closed' to update the Last_Survey_Sent__c field. – Daniel Ballinger Oct 23 '14 at 20:11
  • 1
    But be aware there are problems in your trigger such as adding a duplicate ID Account to the Account list you queried and not limiting the Account query to Accounts related to the Cases and for that matter its unclear why you are querying the Accounts at all. – Keith C Oct 23 '14 at 20:17
  • 2
    @BrianNguyen As stated by Andrew (it should have been a comment), the expected value should be the first parameter to assertEquals or assertNotEquals. This makes the message "read" correctly (e.g. expected today, actual null). You'll find that using it correctly lessens confusion. – sfdcfox Oct 23 '14 at 22:07
1

You have your assert parameters backwards. The first parameter is what you want to be true. The second parameter is the one you are checking against the first. That won't solve the issue with your value being null, but it will at least make the expected value correct.

  • Thank you for the help Andrew. I have switched them and ran the test. Here is the message I am getting: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateLastSurveySent: execution of AfterInsert caused by: System.FinalException: Record is read-only Trigger.UpdateLastSurveySent: line 5, column 1: [] Class.UpdateSurvey.testLastSurveySent: line 18, column 1 – Brian Nguyen Oct 23 '14 at 21:27
  • This does not provide an answer to the question, it would be better to add this as a comment to his question. – Jenny B Oct 23 '14 at 21:33
  • @JennyB, actually this does address the question that was originally in the post. It was why his assert always is expecting null. The question had nothing to do with solving any other problems, just that one. My answer solves that. – Andrew Monshizadeh Oct 23 '14 at 22:36
  • 2
    Honestly, this isn't a great question, it's never going to be useful to someone down the road, it's specific to the code problem. In that respect, this answer is good with me, although the correct answer is "Your test class is failing because your trigger doesn't work" – Ralph Callaway Oct 23 '14 at 23:02
  • @Ralph those are facts. :) – Andrew Monshizadeh Oct 23 '14 at 23:03
0

Based on your comment, I think your trigger should look more like this (untested code):

trigger UpdateLastSurveySent on Case (before insert, before update) {
    Set<Id> accountIds = new Set<Id>();
    for (Case c : Trigger.new) {
        if (c.AccountId != null) {
            accountIds.add(c.AccountId);
        }
    }
    Map<Id, Account> accounts = new Map<Id, Account>([
            SELECT Id
            FROM Account
            WHERE Id in :accountIds
            AND (Last_Survey_Sent__c < LAST_N_DAYS:30 OR Last_Survey_Sent__c = null)
            ]);
    Account[] updates = new Account[] {};
    for (Case c : Trigger.new) {
        if (c.Status == 'Closed' || c.Status == 'Closed - Pending Approval') {
            if (c.AccountId != null && c.Last_Survey_Sent__c != Date.today()) {
                Account a = accounts.get(c.AccountId);
                if (a != null) {
                    c.Last_Survey_Sent__c = Date.today();
                    a.Last_Survey_Sent__c = Date.today();
                    updates.add(a);
                }
            }
        }
    }
    update updates;
}
  • Thanks for the help. I tried testing the trigger and I am receiving the same error in my initial question. The assertion failed as it was expecting today's date, but was receiving null. Any ideas? – Brian Nguyen Oct 23 '14 at 21:58
  • 1
    @BrianNguyen Did you incorporate the change in Daniel Ballinger's comment? The test needs to set values that ensure the main path through the code is taken i.e. all if conditions are satisfied. – Keith C Oct 23 '14 at 22:27
  • Yup I incorporated the change. I set the case status to closed and switched the parameters for the assertEquals. – Brian Nguyen Oct 24 '14 at 15:38
0

Your test class is failing because you're trigger only works on cases with a status of "Closed" or "Closed - Pending Approval", but the case creating by your test method does not set the status.

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.