如何在Nodejs中满足以下单元测试中的测试覆盖率?
How to meet the test coverage in the following Unit testing in Nodejs?
我正在用 NodeJS 进行单元测试。我已经成功地看到了使用 NYC 进行单元测试的范围。我想知道如何扩大我的单元测试范围以增加语句分支和条件分支的覆盖范围。我有以下测试代码。
const value = () => {
const def = "defvalue";
const abc = "abcvalue";
if(abc && abc !== "undefined"){
return abc
}else if (def && def !== "undefined"){
return def
}else{
return false
}
}
//Function to do unit testing.
describe("the result function", () => {
it("should return false", () => {
const result = value();
console.log("The result is:", result);
expect(result).to.be.false;
});
it("should return abc", () => {
const result = value();
console.log("The result is:", result);
expect(result).to.be.eq("abc");
});
it("should return def", () => {
const result = value();
console.log("The result is:", result);
expect(result).to.be.eq("def");
});
});
如果将 def 和 abc 作为参数传递,则可以为每个案例创建一个测试:
const value = (def, abc) => {
if(abc && abc !== "undefined"){
return abc
}else if (def && def !== "undefined"){
return def
}else{
return false
}
}
//Function to do unit testing.
describe("the result function", () => {
it("should return false", () => {
const result = value();
console.log("The result is:", result);
expect(result).to.be.false;
});
it("should return abc", () => {
const result = value("abc", undefined);
console.log("The result is:", result);
expect(result).to.be.eq("abc");
});
it("should return def", () => {
const result = value(undefined, "def");
console.log("The result is:", result);
expect(result).to.be.eq("def");
});
});
我正在用 NodeJS 进行单元测试。我已经成功地看到了使用 NYC 进行单元测试的范围。我想知道如何扩大我的单元测试范围以增加语句分支和条件分支的覆盖范围。我有以下测试代码。
const value = () => {
const def = "defvalue";
const abc = "abcvalue";
if(abc && abc !== "undefined"){
return abc
}else if (def && def !== "undefined"){
return def
}else{
return false
}
}
//Function to do unit testing.
describe("the result function", () => {
it("should return false", () => {
const result = value();
console.log("The result is:", result);
expect(result).to.be.false;
});
it("should return abc", () => {
const result = value();
console.log("The result is:", result);
expect(result).to.be.eq("abc");
});
it("should return def", () => {
const result = value();
console.log("The result is:", result);
expect(result).to.be.eq("def");
});
});
如果将 def 和 abc 作为参数传递,则可以为每个案例创建一个测试:
const value = (def, abc) => {
if(abc && abc !== "undefined"){
return abc
}else if (def && def !== "undefined"){
return def
}else{
return false
}
}
//Function to do unit testing.
describe("the result function", () => {
it("should return false", () => {
const result = value();
console.log("The result is:", result);
expect(result).to.be.false;
});
it("should return abc", () => {
const result = value("abc", undefined);
console.log("The result is:", result);
expect(result).to.be.eq("abc");
});
it("should return def", () => {
const result = value(undefined, "def");
console.log("The result is:", result);
expect(result).to.be.eq("def");
});
});