[英]check if value is a string or a number in mocha/chai test
我很好奇如何使用chai
將value
測試為string
或 number
。 我知道如何為string
或number
編寫測試,當它是嚴格測試時。 但是,當value
可能是其中之一時,如何處理呢?
對字符串的測試:
describe("test", () => {
it("should be a string", () => {
let value = "1234";
value.should.be.a("string");
});
});
對數字的測試:
describe("test", () => {
it("should be a number", () => {
let value = 1234;
value.should.be.a("number");
});
});
是否有一個內置的方式chai
做到這一點?
我知道我可以做一些解決方法,如下所示。 但這感覺有點hacky 。
describe("test", () => {
it("should be a number or string", () => {
let value = 1234;
(typeof value).should.be.oneOf(["string", "number"]);
});
});
你可以編寫自己的方法:
chai.Assertion.addMethod('stringOrNumber', function () {
//Check if it is a string or a number here
});
然后在你的測試中:
expect(myValue).to.be.stringOrNumber();
請參閱插件實用程序文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.