[SalesForce] Writing trigger test to test if custom object was inserted/updated

I am writing an after insert, after update trigger for a custom object CustomA_c. On changing the status field of this custom object to some value I need to create another custom object CustomB_c and insert it.

 trigger triggerOnObj on CustomA__c (after insert, after update) {

    List<CustomA__c> listOfCustomA = [select field1,field2 from CustomA__c where Status__c = 'picklist value'];

    List<CustomB__c> listOfCustomB = new List<CustomB__c>();

    for(CustomA__c customObj: listOfCustomA) {
       CustomB__c customB = new CustomB__c();
       listOfCustomB.add(customB);
       //some other trigger related code
    }
    insert listOfCustomB;
 }

Both the custom objects A and B are unrelated. They do not have look-up to one another.
My problem is when writing a test method/class for this trigger, how do I check if the for loop did inserted that many new B custom objects.

Best Answer

By default, your test class does not see any of the other data in your org, so any CustomObjectB that are inserted will be the only ones there. Once you insert CustomObjectA in your test class, you can SOQL for CustomObjectB and do as follows.

List<CustomB__c> listOfCustomB = [SELECT Id FROM CustomB__c];
System.assertEquals(3, listOfCustomB.size()); //If you think 3 should have been inserted