You need to sign in to do that
Don't have an account?
SFDCDenver
VF how to test the delete link in list
Hello all,
I have a vf page with a hyperlink to delete the record in the list. However, I cannot figure out how to test that portion of the code as if someone clicked it. I can into it but testing stops after this line "SE_Designation__c tobeDeleted = null;" and doesn't hit anything else.
I cannot find any clear documentation on how to accomplish this.
Thanks everyone...
Here is the delete code in the controller.
Here is the test code
I have a vf page with a hyperlink to delete the record in the list. However, I cannot figure out how to test that portion of the code as if someone clicked it. I can into it but testing stops after this line "SE_Designation__c tobeDeleted = null;" and doesn't hit anything else.
I cannot find any clear documentation on how to accomplish this.
Thanks everyone...
Here is the delete code in the controller.
// Cycle through records to locate record to be deleted (ID wrapper) public void DeleteDesignation() { SE_Designation__c tobeDeleted = null; if (SelectedDeleteId != null) // Process if referenced { for(SE_Designation__c d : Designations) // find within the collection { if (d.Id == SelectedDeleteId) { tobeDeleted = d; // Flag record break; // get out } } if (tobeDeleted != null){ delete tobeDeleted; // delete if found refreshDesignations(); // refresh } } }
Here is the test code
static testMethod void DesignationEditorControllerTestMethod() { // Prepare VF page PageReference pageRef = Page.SE_Designation_Section; Test.setCurrentPage(pageRef); // Create a Discovery Discovery__c d = ([Select Id, Name From Discovery__c Limit 1]); // Populate and Intialize controller ApexPages.StandardController sc = new ApexPages.StandardController(d); DesignationEditorController controller = new DesignationEditorController(sc); // Test methods controller.Add(); ApexPages.currentPage().getParameters().put('Rack', 'Test'); controller.Save(); controller.DeleteDesignation(); }
Lastly, it looks to me like you're over complicating the deletedelgation method.
I would do something simple like the following instead:
Thanks for looking at this. I have the SelectedDeletedId cycle the records to locate the record that the user clicked on. So your saying that I can SOQL for the selected record? They can only delete one at a time anyway in the list. Thanks for the headsup.
I agree on the mock, which I will do prior to deployment. I was in a hurry and want to create a utility class that can be called to create records. Still in the design phase of the portion.
Will be trying your code here shortly and update you.
Norm
As far as the SelectedDeletedId field goes, I was implying that you simply need to make sure it's set. Whether it gets set by calling other methods in your controller, or if you manually set it in your test class, it does not matter. What matters is at the time your method is called, if the SelectedDeletedId variable is null, then the logic inside your if block will not execute and will not get code covered.
Now in your test, you could simplify the test by setting SelectedDeletedId = the id of the record you're promissing to create in your unit test :)
My example removes that if block for you by the way, and should get covered regardless if selecteddeletedid is null or not.
Lastly, be sure to add System.assert calls to your unit test to ensure the record has been not only created, but then deleted as a result of calling the DeleteDesignation method too.