Sometimes, we want to stub a Typescript-interface or type definition.
In this article, we’ll look at how to stub a Typescript-interface or type definition.
How to stub a Typescript-interface or type definition?
To stub a Typescript-interface or type definition, we can use the typemoq
library.
For instance, we write
import * as TypeMoq from "typemoq";
//...
let mock = TypeMoq.Mock.ofType<IDependency>();
mock.setup((x) => x.b()).returns(() => "Hello World");
expect(mock.object.a()).to.eq(undefined);
expect(mock.object.b()).to.eq("Hello World");
to import TypeMoq
from typemoq
.
Then we use TypeMoq.Mock.ofType<IDependency>
to create a mocked type from the IDependency
type.
And then we can use the mock
with the content in IDependency
without errors.
Conclusion
To stub a Typescript-interface or type definition, we can use the typemoq
library.