HomeToolsAbout a20k

Testing Inversify

Testing Inversify Classes

Import privates but don't call them

  • Mock them instead before the call

When you are injecting a service that is consumed by a method, do not import or mock downstream dependencies in the service Class constructor

container.snapshot()

Source

// application container is shared by all unit tests import container from "../../src/ioc/container"; describe("Ninja", () => { beforeEach(() => { // create a snapshot so each unit test can modify it without breaking other unit tests container.snapshot(); }); afterEach(() => { // Restore to last snapshot so each unit test takes a clean copy of the application container container.restore(); }); // each test is executed with a snapshot of the container it("Ninja can fight", () => { let katanaMock = { hit: () => { return "hit with mock"; } }; container.unbind("Katana"); container.bind<Something>("Katana").toConstantValue(katanaMock); let ninja = container.get<Ninja>("Ninja"); expect(ninja.fight()).eql("hit with mock"); }); it("Ninja can sneak", () => { let shurikenMock = { throw: () => { return "hit with mock"; } }; container.unbind("Shuriken"); container.bind<Something>("Shuriken").toConstantValue(shurikenMock); let ninja = container.get<Ninja>("Shuriken"); expect(ninja.sneak()).eql("hit with mock"); }); });
© VincentVanKoh