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
mallikharjunarao gundamallikharjunarao gunda 

testclass code coverage for troggers

hal all
this is my trigger code& test class
getting code coverage 100% but method doesnot pass
trigger Accountactive on Account (after update) {
map<id,account>newmap=trigger.newmap;
map<id,account>oldmap=trigger.oldmap;
    list<id>listid=new list<id>();
for(id ids:oldmap.keyset())
{
    if(oldmap.get(ids).Actives__c=='Yes'&&newmap.get(ids).Actives__c=='No')
    {
        listid.add(ids);
    }

 list<contact>cons=[select id,lastname,phone,accountid from contact where accountid=:listid];
 delete cons;
}

testclass
@istest
public class Accountactivefortestclass {
@istest
    static void testme(){
        Account sc=new Account(name='NNN',Actives__c='Yes');
        insert sc;
        contact cs=new contact(lastname='NMN',Accountid=sc.id);
        insert cs;
        string ratings='No';
        account accs=[select id,name,Actives__c from account where id=:sc.id];
        accs.Actives__c=ratings;
       
        update accs;
          system.assertequals(accs.Actives__c,sc.Actives__c);
       
        //account ac=[select id,name,Actives__c from account where id=:sc.id];
       //system.assertEquals('No',ac.Actives__c);
       
        
    }
}
Tanuja JTanuja J
HI,
I have tried this code it shows 100% code coverage.Please check it out and let me know if it is working for you.

@isTest(seeAllData=true)
public class testAccountactive {

    static  testmethod void testAcc(){
        Account acc= new Account(
                     Name='test Account',
                     Active__c ='Yes'
        );
        insert acc;
        
        contact cn = new contact(
                    FirstName='test',
                    lastname ='contact',
                    AccountId =acc.Id);
        
    insert cn;
       account accs =[select id,name,Active__c from Account where Active__c ='Yes' and ID=:acc.Id Limit 1];
        accs.Active__c='No';
        Update accs;
     contact cnn =[select id,name from contact where AccountId = :accs.Id];
        try{
            delete cnn;
        }
        catch(exception e){}
    }

}
 
Ankur Saini 9Ankur Saini 9
Hi mallikharjunarao gunda, 

try this:
 
@istest
public class acb {
@istest
    static void testme(){
        Account sc=new Account(name='NNN',Active__c='Yes');
        insert sc;
        contact cs=new contact(lastname='NMN',Accountid=sc.id);
        insert cs;
        account accs=[select id,name,Active__c from account where id=:sc.id];
        accs.Active__c='No';
       
        update accs;        
    }
}

Thanks 
Ankur Saini
http://mirketa.com