0

For the following class, I have a test class with two methods. The second method is failing at System.assert(Jet2.Status__c == 'Booked'). The status is still 'Active.' How do I fix this?

Trigger

trigger ReservationTrigger on Reservation__c (after insert, after update) {

    Set<Id> JetIds = new Set <Id>();
    List<Jet__c> JetsToSave = new List<Jet__c>();
    List<APEX_Invoice__c> invoicesToSave = new List<APEX_Invoice__c>();
    Map<Id, Jet__c> Jets = new Map<Id, Jet__c>();

    //Go through and if we find the reservation that is marked as Paid,
    //Then make the Jet booked
    //

    if(Trigger.isUpdate){

        for(Reservation__c res: Trigger.new){
            if(res.paid__c != Trigger.oldMap.get(res.id).paid__c && res.paid__c == true){
                JetIds.add(res.Jet__c);
            }
        }

        if(!JetIds.isEmpty()){
            for(Jet__c Jet: [select id,Status__c from Jet__c where id in : JetIds]){
                Jet.Status__c = 'Booked';
                JetsToSave.add(Jet);
            }

            update JetsToSave;
        }   
    }

    if(Trigger.isInsert){
        //Go through the list of reservations and get all of the related jets
        for(Reservation__c Res: Trigger.New){
            Jets.put (Res.Jet__c, null);
        }

        //Now go through the list of jets and add the data into the map
        for(Jet__c Jet: [select Id, Base_Cost__c, Cost_Per_Nautical_Mile__c from Jet__c where Id in :Jets.keySet()]){
            Jets.put (Jet.id, Jet);
        }

        //Go through the list of reservations and create the invoices to write
        for(Reservation__c Res: Trigger.New){

            //Which jet are we using for this reservation
            Jet__c Jet = Jets.get(Res.Jet__c);
            Decimal TotalAmount = Jet.Base_Cost__c + Jet.Cost_Per_Nautical_Mile__c * Res.Nautical_Miles__c;
            APEX_Invoice__c Inv = new APEX_Invoice__c();
            Inv.APEX_Customer__c = Res.APEX_Customer__c;
            Inv.Reservation__c = Res.Id;
            Inv.APEX_Amount_Paid__c = TotalAmount;

            invoicesToSave.add(Inv);
        }
        insert invoicesToSave;
    }
}

Test Class

@isTest
public class ReservationTriggerTestClass {

    static testMethod void testCreateInvoices(){

        Jet__c Jet = new Jet__c(Name                      = 'TestJet1',
                                Base_Cost__c              = 1000,
                                Cost_per_Nautical_Mile__c = 10,
                                Status__c                 = 'Active');

        APEX_Customer__c Cus = new APEX_Customer__c(Name = 'TestCustomer');

        insert Jet;
        insert Cus;

        Reservation__c Res = new Reservation__c(Jet__c            = Jet.id,
                                                APEX_Customer__c  = Cus.id,
                                                Nautical_Miles__c = 50,
                                                Paid__c           = false);


        Test.startTest();
        insert Res;
        Test.stopTest();

        //Check that the invoice was created and the related to the right records
        //

        APEX_Invoice__c Invoice = [select id, Reservation__c, APEX_Customer__c, APEX_Amount_Paid__c
                              from APEX_Invoice__c
                              where APEX_Customer__c = :Cus.Id
                              and Reservation__c = :Res.id];

        System.assert(Invoice.APEX_Amount_Paid__c == 1500);

    }

    static testMethod void testUpdateReservation(){

        Jet__c Jet2 = new Jet__c(Name                     = 'TestJet2',
                                Base_Cost__c              = 1000,
                                Cost_per_Nautical_Mile__c = 10,
                                Status__c                 = 'Active');

        APEX_Customer__c Cus = new APEX_Customer__c(Name = 'TestCustomer2');

        insert Jet2;
        insert Cus;

        Reservation__c Res = new Reservation__c(Jet__c            = Jet2.id,
                                                APEX_Customer__c  = Cus.id,
                                                Nautical_Miles__c = 50,
                                                Paid__c           = false);

        Test.startTest();

        insert Res;
        Jet2 = [select id, status__c from Jet__c where Id = :Jet2.id];
        System.assert(Jet2.Status__c == 'Active');
        Res.Paid__c = true;
        update Res;
        System.assert(Jet2.Status__c == 'Booked');

        Test.stopTest();

    }
}
  • Please use system.assertEquals(expected, actual, message). You will get much clearer failure messages. – Adrian Larson Aug 5 '18 at 18:37
5

DML operations like the update you perform in the code that's being tested have no effect on sObject instances already in memory, except for the population of the Id field on sObjects that are inserted.

    Res.Paid__c = true;
    update Res;
    System.assert(Jet2.Status__c == 'Booked');

Your reference to Jet2 here is stale: it was queried prior to the update operation. You need to re-query it as you do above:

    Jet2 = [select id, status__c from Jet__c where Id = :Jet2.id];

to obtain any changes effected by the trigger.

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.