[Coldbox 6.1][Testbox 4.2.0] How Do You Mock a Dependency When Writing Integration Tests in Testbox?

I’ve got a ColdBox API endpoint that I’m writing some BDD integration tests for. However, the endpoint communicates with a 3rd party API (3rdPartyApi) that I would like to avoid hitting during my tests.

The dependency structure looks something like this:

/handlers/myHandler.cfc
– Depends on: /models/awesomeService.cfc
– Depends on: /models/3rdPartyApi.cfc

My integration test looks something like this:

it( “Can do something awesome”, function(){
var event = get( route = “/api/v1/myHandler/doAwesome” );
var response = event.getPrivateValue( “response” );

expect( response.getError() ).toBeFalse();
expect( response.getStatusCode() ).toBe( 200 );
expect( response.getData() ).toHaveKey( “iAmAwesome” );
} );

My best guess is that I need to access myHandler before I run var event = get(… so I can get the awesomeService and replace 3rdPartyApi with a mocked object. However, I’m not sure how to do that. Has anyone done something like this before? It sounds like a common problem, but I wasn’t able to find any examples in the Testbox documentation or in the Google groups (or perhaps I’m not searching the right terms).

1 Like