Proper way to test Remote Action which leverages helper class to perform a web callout

Another one for the hardcore folks:

I have a Remote Action in a controller extension class.  The Remote Action method invokes a static method on a helper class which then performs a web callout.  The Remote Action has no knowledge of the HTTP request/response ceremony in method it is calling nor should it.

Not quite sure of the proper way to test the Remote Action w/o setting up a HttpCalloutMock implementation.

&#47;&#47; Arrange &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp;&#46;&#46;&#46;&nbsp;<br />Test&#46;setMock(HttpCalloutMock&#46;class, new TRKD_MockHttpResponseGenerator());<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />&#47;&#47; Act<br />String jsonReponse = MyController&#46;doSomethingReturnJSONString();<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />&#47;&#47; Assert<br />System&#46;assert(jsonReponse != null);

It seems to me that I should not have to set up a HttpCalloutMock class since my Remote Action method has no knowledge of the MyController.doSomethingReturnJSONString method implementation i.e. code smell.

Do I change MyController.doSomethingReturnJSONString method to check for Test.IsRunningTest() and return an appropriate response?

Any advice would be appreciated.

Yes, the Remote Action is being invoked from a Skuid page. :slight_smile:

Hi Irvin,

Unfortunately, I can’t think of anything better than the two methods you suggest. I think we typically branch off of Test.isRunningTest() and then provide an alternate response right there in the code. I agree that setting up a Mock for some internal method while testing an outer one smells a little off. :frowning:

Thank you, you confirmed my intuition and olfactics. :slight_smile:  Thanks!