bespoken / virtual-alexa

:robot: Easily test and debug Alexa skills programmatically

Home Page:https://bespoken.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pass handler function directly

schiller-manuel opened this issue · comments

How can I pass a handler function directly into virtual-alexa (instead of passing a string)?

My use case: I need to mock several resources when running the tests locally, hence I want to use a custom handler function purely for testing.

I'm sure I'm probably missing the point here, but if you simply provide a different name ("index.customFunction") that will call another function.

Sure I could actually write this function to a file, but I'd like to create a function locally inside my test and then pass it to virtual-alexa, e.g.:

const myTestHandler = (event, context, callback) => {
    const alexa = Alexa.handler(event, context, callback);
    alexa.registerHandlers(mockHandler);
    alexa.execute();
};

const virtualAlexa = VirtualAlexa.Builder()
    .handler(myTestHandler)
    .interactionModelFile('model.json')
    .create();

I see now - that's a nice use-case. I added a PR for this, along with a pre-release version you can try out: 0.3.11-0.

The PR is here: #22

An example of how to use it is here:

const myFunction = function(event: any, context: any) {

Very similar to what you have above. Give it a try and let me know how it goes.

works perfectly, thanks for the rapid implementation :)

Awesome - glad to hear it. Thanks for the suggestion, and thanks also for the other PR.