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

How to use system.assert on a returned list?

I'm building a vf page that will show details of a parent object, and then iterate over a list of related child records that have a certain status (where status__c != 'Leased').


I'm looking for an explanation on how to use System.Assert in regards to the related list of child objects.


Meaning, I have a query method that should result in a list of related child records.


So...I write a testmethod that calls my query method.


When I call my query method and it returns a list how, then, do I use System.Assert to assert that my query method has returned the list I expect?


All the examples I have seen of System.Assert seem to check one value against another. In my case, I'm not checking one value against another, I just want to verify that the query method has returned a list of related child objects where status__c != 'Leased'.


If there are no related child records where status__c != 'Leased', that is fine. I just won't display anything out on the vf page - that section will just be blank. Likewise, I don't really care how many related child records are returned in the list either. Meaning, the query method could return 1 record in the list or it could return 100 records in the list. Doesn't matter.


Furthermore, I'm using the vf page as part of a Site. Since it is public, I'm not allowing any saving, updating or editing of either the parent record being viewed or the related child objects being viewed. The vf page is only intended to query the db for related child records (where status__c != 'Leased') and iterate over them on the vf page in read-only mode. No data manipulation happening.

Best Answer chosen by Admin (Salesforce Developers) 

1. Create a new parent record in your test method and then run your test method for that parent record.

2. System.assert(parent.childRecords.size()==0).


Essentially you are asserting that if no data is there for the child the size of the list should be 0.


I would repeat the same test populating some child records and asserting the size suitably.