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
SFDCDenverSFDCDenver 

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.
// 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();
    }



 
Mathew Andresen 5Mathew Andresen 5
Can't you just query for the deleted Id and test to see if it's returned or not?
James LoghryJames Loghry
Your test method looks like it's relying on existing data, which it should not be.  Instead, you should create mock records (for instance a new Discovery__c record).  Also, it looks like your delete delegation method depends on a member variable called SelectedDeleteId, which will need be set either in your unit test or somewhere else, depending on the logic of your controller.   Once you set that variable, it should kick off the logic in your if statement.

Lastly, it looks to me like you're over complicating the deletedelgation method.

I would do something simple like the following instead:
 
// Cycle through records to locate record to be deleted (ID wrapper)
public void DeleteDesignation() {
    delete [Select Id From SE_Designation__c Where Id = :SelectedDeletedId];
    refreshDesignations();
}



 
SFDCDenverSFDCDenver
James,

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
James LoghryJames Loghry
Yep, you can use SOQL to query the record, and then delete it all in one swoop as in the example I posted above.  SOQL is smart enough to return just a single record or a list of records, depending on the type of the variable you're assigning it to.  The example above, you're really deleting an array of records, but it will only be an array of 1 record since you're filtering on the unique Id field.

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.