function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tommy GeorgiouTommy Georgiou 

Test Class Assertion Failed: Expected: Sent, Actual: draft

Hi All,

I've written a trigger for Orders where whenever I send an email through the Order the status in the order from 'draft' will become 'sent'. And it works like a charm. My trigger is :
 
trigger TaskTrigger on Task (after insert) {
    List<Order> ordersToUpdate = new List<Order>();

    for(Task t : Trigger.new) {
        if(t.WhatId.getSObjectType() == Order.sObjectType 
            && t.Subject.startsWith('Email:')) {
                ordersToUpdate.add(new Order(Id = t.WhatId, Status = 'Sent'));
        }
    }

    update ordersToUpdate;
}

My problem is with the test class. After modifying my calss alot of times I came to a result where System.AssertException: Assertion Failed: Expected: Sent, Actual: draft

My class is 
@isTest
   public class Test7{
    public static testmethod void TaskTrigger_Test1()
    {
             Account a = new Account(Name = 'Test');
        insert a;     

        Id pricebookId = Test.getStandardPricebookId();                         

        Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1', isActive = true);
        insert prd1;

        PricebookEntry pe=new PricebookEntry(UnitPrice = 1,Product2Id=prd1.id,Pricebook2Id=pricebookId,isActive=true);
        insert pe;

        Order o = new Order(name='Test1',AccountId=a.id,EffectiveDate=system.today(),status='draft', PriceBook2Id=pricebookId);
        insert o;

        OrderItem oi = new OrderItem(OrderId=o.id,Quantity=1,PricebookEntryId=pe.id, unitPrice=1);
        insert oi;


        Task t = new Task(whatid=o.id,Priority = 'normal',status='open',subject='Email:xxxx');
        insert t;
        
        system.assertequals('Sent',o.status);
    }


}

 
Best Answer chosen by Tommy Georgiou
EMHDevEMHDev
Hi Tommy,
In order to check whether your trigger executed as expected in a test method, you need to do a query to get the resulting record back again.  So you need something like 
Order oret = [select status from Order where AccountId = :a.id];

and then assert on oret, not your original o.

All Answers

EMHDevEMHDev
Hi Tommy,
In order to check whether your trigger executed as expected in a test method, you need to do a query to get the resulting record back again.  So you need something like 
Order oret = [select status from Order where AccountId = :a.id];

and then assert on oret, not your original o.
This was selected as the best answer
Tommy GeorgiouTommy Georgiou
Thanx alot. Been struggling for days with it
 
EMHDevEMHDev
We all did this when learning Apex! And I also spent many hours trying to puzzle it out.