I’m working on a new community module for Forgebox and I’ve got handlers within the module which announce several interception methods. I’m writing my integration tests, and I’d like to test whether these interceptor methods get announced successfully. How do I best do that?
Here’s my theory about what needs to be done, but I’m struggling with the implementation:
- Create a new test spec
- Mock a new interceptor object and create methods that I will listen for
- Use the
$spy()
method so I can check how many times that method has been called.
Questions:
- Assuming my approach is correct, how would I go about registering this mocked interceptor in the test-harness?
- Is there a better way to do this? I know Testbox has a BaseInterceptor test, but I think that’s used for testing pre-existing interceptors, so I’m not really sure if that’s what I want or not.
Sample Handler Interceptor Call:
// Main.cfm handler in the module
function index( event, rc, prc ) {
// announce the login attempt
announce( "onLoginAttempt", {
"username": rc.username,
"success": loginAttemptSuccess,
"message": loginAttemptMessage
} );
}
Update:
I’ve made some progress. Since we’re dealing with an integration test, we need to enable mocking on the InterceptorService
, which is available in the controller. The code below enables mocking and spies on the “announce” method. Then we filter the call log for the events we expect. We have to filter because otherwise there’s a million other methods in there related to other aspects of the framework.
Code:
// MainTest.cfc Module's integration test
it( "Fires the expected interceptor methods", function() {
// get the interceptor service and spy on the announce method
var interceptorService = prepareMock( controller.getInterceptorService() );
interceptorService.$spy( "announce" );
// define the mock methods we expect to be called
var mockMethods = [
"onLoginInit",
"onLoginAttempt",
"onLogOut",
"onLoginLayoutHead",
"onLoginAlreadyBody"
];
// now execute the login action
var event = execute(
route = baseUrl,
renderResults = true
);
expect( event.getStatusCode() ).toBe( 200 );
// filter the call log to only include the login interceptors
var filteredLog = interceptorService.$callLog().announce.filter( function( item ) {
return mockMethods.findNoCase( item[ 1 ] );
} );
// our filtered call log
debug( var=filteredLog, top=2 );