容器快照
声明容器快照可以帮助您轻松编写单元测试:
import { expect } from "chai";
import * as sinon from "sinon";
import container from "../../src/ioc/container";
describe("Ninja", () => {
beforeEach(() => {
container.snapshot();
});
afterEach(() => {
container.restore();
});
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");
});
});