我的测试在一起运行时失败,但在单独运行时通过
这是主要功能,当我将密码条件OBJ放置在密码串联功能的所有测试中时,我注意到了所有测试。
function passwordIsValid(password) {
if (password.length == 0) {
throw errorMessages.passwordNotFound;
}
if (password.length < 9) {
throw errorMessages.invalidLength;
}
for (const condition in passwordConditions) {
if (!passwordConditions[condition].test(password)) {
throw errorMessages[condition]();
}
}
return true;
}
助手功能
const passwordConditions = {
"lowercase letter": /[a-z]/g,
"uppercase letter": /[A-Z]/g,
"digit ": /[0-9]/g,
"special character": /[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g,
"whitespace character": /\s/g,
};
const errorMessages = {
passwordNotFound: "password should exist",
invalidLength: "password should be longer than 8 characters",
"lowercase letter": () =>
`password should have at least one lowercase letter`,
"uppercase letter": () =>
`password should have at least one uppercase letter`,
"digit ": () => `password should have at least one digit `,
"special character": () =>
`password should have at least one special character`,
"whitespace character": () =>
`password should have at least one whitespace character`,
errorType: () => Object.keys(passwordConditions),
errorRegex: () => Object.values(passwordConditions),
};
这些是测试
describe("passwordChecker.js", () => {
describe("passwordIsValid", () => {
it("should return true", () => {
expect(passwordIsValid("p@s5 Words")).toBe(true);
});
it("should return password should exist", () => {
expect(function () {
passwordIsValid("");
}).toThrow(errorMessages.passwordNotFound);
});
it("should return password should be longer than 8 characters", () => {
expect(function () {
passwordIsValid("P@s5 ");
}).toThrow(errorMessages.invalidLength);
});
it("should return password should have at least one lowercase letter", () => {
expect(function () {
passwordIsValid("P@S5 WORDS");
}).toThrow(errorMessages["lowercase letter"]());
});
it("should return password should have at least one uppercase letter", () => {
expect(function () {
passwordIsValid("p@s5 words");
}).toThrow(errorMessages["uppercase letter"]());
});
it("should return password should have at least one digit", () => {
expect(function () {
passwordIsValid("P@ss words");
}).toThrow(errorMessages["digit "]());
});
it("should return password should have at least one special character", () => {
expect(function () {
passwordIsValid("Pas5 Words");
}).toThrow(errorMessages["special character"]());
});
it("should return password should have at least one whitespace character", () => {
expect(function () {
passwordIsValid("p@s5Words");
}).toThrow(errorMessages["whitespace character"]());
});
});
});
失败的测试,因为错误的错误被丢弃,我无法弄清楚为什么错误的错误被丢弃:
“预期的'密码应包含至少一个whitespace字符'要投掷','密码应包含相反,至少一个大写字母'
是“有第二个功能也会导入相同的助手功能受到影响。
function passwordStrength(password) {
const strength = ["weak", "medium", "strong"];
let strengthValue = -1;
if (password.length < 9) {
return "invalid";
}
errorMessages.errorRegex().map((condition) => {
if (condition.test(password)) {
strengthValue++;
}
});
return strength[strengthValue > 2 ? 2 : strengthValue > 0 ? 1 : 0];
}
第二个功能的测试受到其他功能测试的影响
describe("passwordChecker.js", () => {
describe("passwordStrength", () => {
it("should return invalid if condition 1 or 2 are not met", () => {
expect(passwordStrength("p@s5word")).toBe("invalid");
});
it("should return weak if 3 conditions are met", () => {
expect(passwordStrength("passwords")).toBe("weak");
});
it("should return medium if 4 or 5 conditions are met", () => {
expect(passwordStrength("PassWords")).toBe("medium");
});
it("should return strong if 6 or more conditions are met", () => {
expect(passwordStrength("P@s5 Words")).toBe("strong");
});
});
});
“预期弱为中等”
This is the primary function, I have noticed when I place the passwordConditions obj inside the passwordIsValid function all the tests pass.
function passwordIsValid(password) {
if (password.length == 0) {
throw errorMessages.passwordNotFound;
}
if (password.length < 9) {
throw errorMessages.invalidLength;
}
for (const condition in passwordConditions) {
if (!passwordConditions[condition].test(password)) {
throw errorMessages[condition]();
}
}
return true;
}
Helper functions
const passwordConditions = {
"lowercase letter": /[a-z]/g,
"uppercase letter": /[A-Z]/g,
"digit ": /[0-9]/g,
"special character": /[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g,
"whitespace character": /\s/g,
};
const errorMessages = {
passwordNotFound: "password should exist",
invalidLength: "password should be longer than 8 characters",
"lowercase letter": () =>
`password should have at least one lowercase letter`,
"uppercase letter": () =>
`password should have at least one uppercase letter`,
"digit ": () => `password should have at least one digit `,
"special character": () =>
`password should have at least one special character`,
"whitespace character": () =>
`password should have at least one whitespace character`,
errorType: () => Object.keys(passwordConditions),
errorRegex: () => Object.values(passwordConditions),
};
These are the tests
describe("passwordChecker.js", () => {
describe("passwordIsValid", () => {
it("should return true", () => {
expect(passwordIsValid("p@s5 Words")).toBe(true);
});
it("should return password should exist", () => {
expect(function () {
passwordIsValid("");
}).toThrow(errorMessages.passwordNotFound);
});
it("should return password should be longer than 8 characters", () => {
expect(function () {
passwordIsValid("P@s5 ");
}).toThrow(errorMessages.invalidLength);
});
it("should return password should have at least one lowercase letter", () => {
expect(function () {
passwordIsValid("P@S5 WORDS");
}).toThrow(errorMessages["lowercase letter"]());
});
it("should return password should have at least one uppercase letter", () => {
expect(function () {
passwordIsValid("p@s5 words");
}).toThrow(errorMessages["uppercase letter"]());
});
it("should return password should have at least one digit", () => {
expect(function () {
passwordIsValid("P@ss words");
}).toThrow(errorMessages["digit "]());
});
it("should return password should have at least one special character", () => {
expect(function () {
passwordIsValid("Pas5 Words");
}).toThrow(errorMessages["special character"]());
});
it("should return password should have at least one whitespace character", () => {
expect(function () {
passwordIsValid("p@s5Words");
}).toThrow(errorMessages["whitespace character"]());
});
});
});
The tests fail because the wrong error gets thrown, I can't figure out why the wrong error is being thrown:
"expected 'password should contain at least one whitespace character' to be thrown, 'password should contain at least one uppercase letter' was thrown instead"
There is a second function that imports the same helper functions being affected as well.
function passwordStrength(password) {
const strength = ["weak", "medium", "strong"];
let strengthValue = -1;
if (password.length < 9) {
return "invalid";
}
errorMessages.errorRegex().map((condition) => {
if (condition.test(password)) {
strengthValue++;
}
});
return strength[strengthValue > 2 ? 2 : strengthValue > 0 ? 1 : 0];
}
The tests for the second function are being affected by the other functions tests
describe("passwordChecker.js", () => {
describe("passwordStrength", () => {
it("should return invalid if condition 1 or 2 are not met", () => {
expect(passwordStrength("p@s5word")).toBe("invalid");
});
it("should return weak if 3 conditions are met", () => {
expect(passwordStrength("passwords")).toBe("weak");
});
it("should return medium if 4 or 5 conditions are met", () => {
expect(passwordStrength("PassWords")).toBe("medium");
});
it("should return strong if 6 or more conditions are met", () => {
expect(passwordStrength("P@s5 Words")).toBe("strong");
});
});
});
"expected weak to be medium"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论