florinn/typemoq

Setup call to callback

jimmy-anttila opened this issue · 2 comments

I have a class with a method which has a callback as one of the arguments.

class SomeClass {
    public someMethod(callback: (message: string) => void): void {
        callback("hello");
    }
}

How can I setup a mock for someMethod that would invoke the callback with another value, like "hello mock" for example?

const mock = Mock.ofType(SomeClass);
mock.setup((x) => x.someMethod(what do I here?));

Hi @jimmy-anttila ,

I had a quite similar issue and if you want to do that, try this:

const mock = Mock.ofType(SomeClass);
mock.setup((x) => x.someMethod(typemoq.It.isAny())).returns((callback) => callback("hello mock"));

With that you will call your callback with the value "hello mock".

Say me if it's want you expected.

That works as expected, thanks for your input.