+ Start a Discussion
King KooKing Koo 

Controller Extension Test - action method in actionSupport not invoked automatically?

Hi there

Assuming I have two picklists in my VF page, something like this:

<apex:selectList value="{!parentId}" id="cmbParent" size="1" required="true" label="Parent">
  <apex:selectOptions value="{!parents}" />
  <apex:actionSupport event="onchange" action="{!clearChild}" reRender="cmbChild"/>
</apex:selectList>
<apex:selectList value="{!childId}" id="cmdChild" size="1" label="Child">
  <apex:selectOptions value="{!children}" />
</apex:selectList>

In a nutshell, if someone changes the value of the parent picklist, that should call the action method clearChild() and then re-render the child picklist.

I would like to know, in the test class, even if I have called the Test.setCurrentPage, if I programmatically change the parentId, is that not supposed to call the action method automatically?

i.e.

TestCase__c  testcase = new TestCase();
Test.setCurrentPage(Page.MyTestPage);

ApexPages.standardController std = new ApexPages.standardController(testcase);
MyTestCaseExt controllerExt = new MyTestCaseExt(std);

controllerExt.parentId.value = 3;

// after the above line, can I count on the action method clearChild getting called?
// Or do I need to call it explicitly?
//controllerExt.clearChild();
System.assertEquals(null, controllerExt.childId);

I'm asking because it seems to me the action method does not actually get called even though I have specifically set the page reference.

Originally I thought when the parent picklist value changes, it will automatically call the action method to clear the content of the child picklist.  If I have to call the action method myself, then obviously my assert will be successful.  It'll be like, I set variable a to 3, and then I check if a equals 3, which is kind of pointless, is it not?

Thanks
King
MJ Kahn / OpFocusMJ Kahn / OpFocus
Calling Test.setCurrentPage is useful if your controller gets values from the page's query string parameters, but it does not cause any of controller's action methods to be called automatically - your unit tests still have to call the action methods manually.