0

I can get the trigger (before update) to update the record accurately in the scenario I'm testing. But, I can't get this test to pass. Any help would be greatly appreciated.

@isTest 
private class OpportunityTest2 {

    static testMethod void myWinTriggerTest() {

        // Create an account with an opportunity, 
        Account acct = new Account(Name='Test Account',
                                  CreatedDate = System.today().addDays(-3),
                                  First_Order_Date__c=System.today());
        insert acct;
        Opportunity opp = new Opportunity(Name='Test Opportunity',
                                       StageName='Qualified',
                                       CloseDate=System.today().addMonths(2),
                                       AccountId=acct.Id);
        insert opp;

        test.startTest();
        opp.Ready_to_Close__c = true;

        update opp;         

        test.stopTest();
        System.assert(opp.IsWon);
        System.assertEquals('Closed Won', opp.StageName);
    }
} 
1
  • Where/How is it failing? Any Errors? Also, can you share the relevant parts of the trigger? Commented Apr 11, 2018 at 0:37

1 Answer 1

3

Without seeing the error, i am not sure where this is failing. However, I spotted something right away.

After doing your update, you must re-query the record in order to see its changed values.

So, your code should do something like this:

    test.startTest();
    opp.Ready_to_Close__c = true;

    update opp;         

    test.stopTest();
    //NEW LINE
    opp = [select IsWon, StageName from Opportunity where id = :opp.id limit 1];
    System.assert(opp.IsWon);
    System.assertEquals('Closed Won', opp.StageName);

That way the object opp will have updated values for the fields and your asserts will work (assuming your logic is solid)

2
  • 1
    Beat me to it, good catch!
    – David Reed
    Commented Apr 11, 2018 at 0:39
  • You’re welcome! Commented Apr 11, 2018 at 4:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .