• Brian Nguyen 2
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

Hi,

I have wrote a trigger that sets a field call "Last Survey Sent" on the Account equal to the "Last Survey Sent" field on the Case. The field on the Case gets set via workflow when a survey is sent to a customer. As I am testing the apex class, I am getting the error: "System.AssertException: Assertion Failed: Expected: null, Actual: 2014-10-10 00:00:00
Class.UpdateSurvey.testLastSurveySent: line 21, column 1". Any ideas on how to resolve this issue?

Trigger:
Trigger UpdateLastSurveySent on Case (after insert, after update) {
    List<Account> accList = new List<Account>();
    for (Case c : Trigger.new) {
        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('Could not update Last Survey Sent field on Account with cause: ' + ex.getCause());
    }


Apex Class:
@IsTest   
public class UpdateSurvey {
    @isTest static void testLastSurveySent() {
   
        // Insert Account and Case for tests
        // These may need additional fields, depending on your organization
        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 that the Last Survey Sent field on the account was updated on 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 that the Last Survey Sent field on the account changes on 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());
    }
}