bespoken / virtual-alexa

:robot: Easily test and debug Alexa skills programmatically
https://bespoken.io
Apache License 2.0
112 stars 35 forks source link

pass handler function directly #20

Closed schiller-manuel closed 6 years ago

schiller-manuel commented 6 years ago

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.

jkelvie commented 6 years ago

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.

schiller-manuel commented 6 years ago

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();
jkelvie commented 6 years ago

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: https://github.com/bespoken/virtual-alexa/blob/3d743630c71f76794f7bd288ccc5e4c298414cd2/test/VirtualAlexaTest.ts#L507

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

schiller-manuel commented 6 years ago

works perfectly, thanks for the rapid implementation :)

jkelvie commented 6 years ago

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