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
Vinay JVinay J 

Test class for trigger

Dear firends,

I have two objects in lookup relationship.. Auction and Bid where bid is child.... Whenever a Auction's status is changed to Ended, amount field from bid object is copied to Auction object's field amount by a trigger.. Now, I'm writting test class where I'm creating Auction and the child Bid record..

         Auction__c auction = new Auction__c();
                auction.Auction_Status__c = 'Active';
                auction.Bid_Amount__c = 0;
                insert auction;
        
        Bid__c bid = new Bid__c();
                bid.Auction__c = auction.id;
                bid.Bid_Amount__c = 10;
                insert bid;
        
        auction.Auction_Status__c = 'Ended';
        update auction;
        
        system.debug('*****Auction = ' + auction);
        system.debug('*****Bid = ' + Bid);

The trigger work fine when I test from UI i.e. when I update Auction's status to Ended, Amount from bid is copied to amount on Auction. But in by test class, The debug still gives me amount as 0.

Can someone please help me in understanding why? Debug in trigger also shows the correct amount.
pconpcon
This is because you need to re-query the bid object
 
Bid__c updatedBid = [ select Bid_Amount__c from Bid__ where Id = :bid.Id ];

Then do you assert on updatedBid to make sure it was actually updated.
Vinay JVinay J
Thanks pcon for your help.. I have one more doubt.. when Auction ends, trigger creates payment__c record as well.. but after the debugs, if I'm tryting to query payment__c, I'm getting null.. any idea why?
pconpcon
Can you please provide the whole code you are using to including the part that you are trying to query payment with?
Vinay JVinay J
Yup.. So this is what happens in Auction trigger apart from other things......

for(Auction__c endedAuction : listEndedAuctions) {
            
            //For all ended auctions, create a list of consumer payments to be created.
                            Payment__c accountPayment = new Payment__c();
             
                accountPayment.Account__c = endedAuction.Registered_Charity__c;
                accountPayment.Payment_Amount__c = endedAuction.Bid_Amount__c;
                accountPayment.Payment_Status__c = sPending;
                accountPayment.Payment_Date__c = date.newInstance(dPresentDate.year(), dPresentDate.month(), 1);
add to payment__c list..

        }
insert list;
       

In the test class, I've written like..

Account acc = new Account();
acc.name = 'Test';
insert acc;

Auction__c auction = new Auction__c();
                auction.Auction_Status__c = 'Active';
                auction.Bid_Amount__c = 0;
auction.account = acc.id;
                insert auction;
        
        Bid__c bid = new Bid__c();
                bid.Auction__c = auction.id;
                bid.Bid_Amount__c = 10;
                insert bid;
        
        auction.Auction_Status__c = 'Ended';
        update auction;
        
bid__c bid1 = [Select id, bid_amount__c from bid__c where auction__c = : auction.id];
        system.debug('*****bid1 = ' + bid1);
        
        payment__c pay = [Select id, name, payment_amount__c from payment__c where account__c = :acc.id];
        system.debug('*****pay = ' + pay);

*************************

on the query.. payment__c pay = [Select id, name, payment_amount__c from payment__c where account__c = :acc.id]; , I get error list has no row for assignment....

But the functionality is working fine when I tested from UI... Payment did get created when a acution record is updated to Ended.
pconpcon
The problem is that your test is setting the wrong account field on the auction.
 
Account acc = new Account();
acc.name = 'Test';
insert acc;

Auction__c auction = new Auction__c();
auction.Auction_Status__c = 'Active';
auction.Bid_Amount__c = 0;
auction.Registered_Charity__c = acc.id;
insert auction;
    
Bid__c bid = new Bid__c();
bid.Auction__c = auction.id;
bid.Bid_Amount__c = 10; 
insert bid;
    
Test.startTest();

auction.Auction_Status__c = 'Ended';
update auction;

Test.stopTest();
    
bid__c bid1 = [select bid_amount__c from bid__c where auction__c = : auction.id];
    
payment__c pay = [select name, payment_amount__c from payment__c where account__c = :acc.id];
//System.assertEquals goes here