You need to sign in to do that
Don't have an account?
Raghu Sharma 6
delete a record test class is failing
I have created a custom button on account to delete current account record when some conditions are met. When I click on custom button, it calls Apex Page which in turn calls Controller extension to delete the record. I have written below test class to test it. code is working good when I try to delete the record from VF page but when I try the same in test class, record is not getting deleted. System.assertequals is failing as isdeleted is returning 'false'. What am I missing here?
My VFPage: <apex:page StandardController="Account" Extensions="MyCtrlextn" action="{!FunctiontoDelete}"> <apex:PageMessages /> </apex:page>
My Apex Class: public class MyCtrlExtn { private ApexPages.standardcontroller stdCtrl; public MyCtrlExtn(ApexPages.Standardcontroller std) { stdCtrl=std; } public PageReference FunctionToDelete() { Account Acc =(Account) stdctrl.getRecord(); return stdCtrl.delete(); } }
My Test Class: @isTest private class testClass { static Account testAcc = new Account (); testAcc.Name = 'TestAccount'; insert testAcc; static testMethod void testDel() { Test.startTest(); Test.setCurrentPage(Page.MyVFPage); ApexPages.StandardController sc = new ApexPages.StandardController(testAcc); MyCtrlExtn TE = new MyCtrlExtn(sc); Pagereference pr = TE.FunctionToDelete(); Account DelAcc = [select id,isdeleted from Account where id =: testAcc.id All rows]; System.Assertequals(DelAcc.isdeleted, true); Test.stopTest(); } }
You could navigate to any other view using PageReference instance read this: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_system_pagereference.htm
so you could create instance and return it.
Thanks
Shashikant
All Answers
Test Class Data is virtually created and is not commited to database. So when you delete it it does not reach to Recyclebin either. In order to assert your result you should do
Thanks
Shashikant
Try selecting that account before deleting:
Account acct = [SELECT ID FROM Account where id =: testAcc.id LIMIT 1];
so you your code should look this this:
Class Code: VFP - remove page action
Test Class - I have asserted both result before delete and after calling the method to delete.
Thanks
Shashikant
You could navigate to any other view using PageReference instance read this: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_system_pagereference.htm
so you could create instance and return it.
Thanks
Shashikant