+ Start a Discussion
goabhigogoabhigo 

Some clarifications needed on Test classes

Hi,

 

I have been hounded by the following questions. I think I know answers to couple of them, but would like to confirm.

 

According to Testing Best Practices, we need to use System.assert methods to prove code behaves properly. Here is a scenario I need answers:

  • My trigger on a child object updates parent fields (kind of roll up, I can't use master-detail)
  • 1st I inserted Account (parent), then inserted Branches, says 5 (child)
  • The trigger runs on insert and updates 'No. of Branches' field of Account
  • Initially when I inserted Account, No. of Branches was 0. Now it should be 5
  • System.debug statements on trigger shows me that Account was updated successfully BUT when I use system.assertEquals(acc.No_of_Branches__c, 5) it fails
  • The System.debug(acc) shows me that the value is still 0, but when I query (as per http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods - Test.startTest/Test.stopTest section) and see the field is updated

Why?? Do I need to do something extra? Is there any document which explains this behaviour?

 

To test the code for bulk records I use loops (for or while) and insert list of records. Is this the way? I understand that we can also use Test.loadData().

bob_buzzardbob_buzzard
The version of the account in your test class won't be updated, the trigger gets a separate instance of the record. The only exception to this is the I'd is populated when you insert.
goabhigogoabhigo

Thanks Champ. But why is this behavior? Why separate instance? Apologies if I am troubling on weekend, but these questions are hounding me!

I saw your twitter reply. Please let me know if you can find a doc. I will try to connect with Salesforce meanwhile.

goabhigogoabhigo

I also noticed that while testing a controller, the initialization of VF page (which is paginated) doesn't call the variables that are used in apex output texts. Is it expected behaviour?

Also calling next() methods doesn't recreate/update the list thats shown in the pageBlockTable. But when I check in UI the next() action refreshes the table and next set of records are shown.

goabhigogoabhigo

Ok got the answer. http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods, under Test Methods and Visualforce Controllers.

 

//Example of calling the 'setter' method for several properties. 
//Normally these setter methods are initiated by a user interacting with the Visualforce page, 
//but in a test method, just call the setter method directly. 

Realising the effects of keeping myself away from coding ;) A mistake that I will never repeat!

@altius_rup@altius_rup
When you expect to test results on an object you have just inserted, always re-query it just after insert like this :

acc = [ SELECT Id, this__c, that__c FROM Account WHERE Id= :acc.id];

You're sure to get correct test results.
Beware, however, of missing attributes in SOQL, if you have forgotten other__c for example :(

Rup