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
Robert WynterRobert Wynter 

Please Help a newbiee create a test class for the following trigger

It's been a long while since I looked into written Apex triggers. I successfully created one a year ago but neglected to creat a test class for it which has now come to bite me in th ARS. If someone can have pitty on me and help me create a test class that would be greatly appreciated
 
trigger updateAccountStatus on Opportunity (after insert, after update) 
{ 
list<Id> accIds = new list<Id>(); 

/* list<Account> accounts = new list<account>(); */ 
Map<Id, Account> accounts = new Map<Id, account>(); 
for(opportunity o:trigger.new){ 

accIds.add(o.accountId); 

} 

for(account a:[select Id, Status__c from account where Id IN :accIds]){ 

for(opportunity opp:trigger.new){ 

a.Status__c = opp.Status__c; 
a.Approval_Progress__c = opp.StageName; 

/*accounts.add(a);*/ 
accounts.put(a.Id, a); 
/* if(opp.Status__c=='Active'){ 

a.Status__c='Active'; 

accounts.add(a); 

} 
*/ 
} 

} 
update accounts.values(); 
/* update accounts;*/ 
}

 
Balaji BondarBalaji Bondar
Hi Robert,
Please refer below link for test class creation for trigger.
https://developer.salesforce.com/docs/atlas.en-us.198.0.apexcode.meta/apexcode/apex_qs_test.htm

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
Robert WynterRobert Wynter
I think I might have something based on that link. Does the following make sense:
 
@isTest
public with sharing class testUpdateAccountStatus {
    static testMethod void LogPersonAccountChangeTest() {
        Account account = new Account(Name='TEST AGENT ACCOUNT',Status__c='Active');
        System.debug('Status before inserting new Status: ' + account.Status__c);

       // Insert book

       insert account;
        
        // Retrieve the new book
        account = [SELECT Status__c FROM Account WHERE Id =:account.Id];
        System.debug('Status after trigger fired: ' + account.Status__c);
    
           // Test that the trigger correctly updated the price
      	   System.assertEquals('Active', account.Status__c);
        
        }
}