我创建了一个装饰器,它包含一个规则,当它被一个类多次使用时抛出,它的真正基本版本将如下所示:
function someDecorator(target: any) {
var msg = "Decorator can only be applyed once";
if(target.annotated != undefined) throw new Error(msg);
target.annotated = true;
}
因此,如果开发人员尝试多次使用开发人员,则会抛出:
@someDecorator
@someDecorator // throws
class Test {
}
我希望编写单元测试以验证此功能,一切正常。我使用的是mocha,chai和sinon。
我如何断言someDecorator会抛出?
答案 0 :(得分:0)
我如何断言someDecorator会抛出?
您基本上想要两次(someDecorator(someDecorator(function(){})
)和assert a throw。
答案 1 :(得分:0)
另一种可能的方法:
declare function __decorate(decorators, target, key?, desc?);
declare function __param(paramIndex, decorator);
describe("@named decorator \n", () => {
it("It should throw when applayed mutiple times \n", () => {
var useDecoratorMoreThanOnce = function() {
__decorate([
__param(0, named("a")),
__param(0, named("b"))
], NamedTooManyTimesWarrior);
};
var msg = "Metadadata key named was used more than once in a parameter.";
expect(useDecoratorMoreThanOnce).to.throw(msg);
});
});