expect in chai 如何处理文本字符串?
How does expect in chai work with a text string?
我正在尝试了解 chai.js 中的期望行为。我有代码来检查登录是否因凭据无效而失败。这是我的代码:
describe('Login', function() {
before(function(done) {
driver.get('https://pluma-dev.herokuapp.com/client-sign-in').then(done);
});
it('Login with Incorrect credentials', function( done ) {
driver.findElement(webdriver.By.name("username")).sendKeys("kushal.d.joshi+29@gmail.com");
driver.findElement(webdriver.By.name("password")).sendKeys("123");
driver.findElement(webdriver.By.className("client-onboarding-signin-btn")).click().then(function(){
driver.findElement(By.css(".has-error")).getText().then(function (text) {
console.log(text);
try{
expect(text).to.be.a("Invalid email or password");
done();
} catch (e) {
done(e);
}
});
});
});
});
根据我的理解,这个测试用例应该通过,因为我期待无效的用户名和密码并且得到了相同的。但是,它会抛出断言错误。那是,
1) 使用不正确的凭据登录登录:
AssertionError:预期 'Invalid email or password' 是无效的电子邮件或密码
您使用了错误的断言。您要使用:
expect(text).to.equal("Invalid email or password");
.to.be.a
(实际上只是对名为 a
的断言的调用)断言该值具有特定的 类型 。这是一些说明差异的代码:
const expect = require("chai").expect;
it("a", () => expect("foo").to.be.a("foo"));
it("equal", () => expect("foo").to.equal("foo"));
it("correct use of a", () => expect("foo").to.be.a("string"));
第一个测试将失败。第二个和第三个是正确的用法,所以通过了。
您可以找到 a
here 的文档。
我正在尝试了解 chai.js 中的期望行为。我有代码来检查登录是否因凭据无效而失败。这是我的代码:
describe('Login', function() {
before(function(done) {
driver.get('https://pluma-dev.herokuapp.com/client-sign-in').then(done);
});
it('Login with Incorrect credentials', function( done ) {
driver.findElement(webdriver.By.name("username")).sendKeys("kushal.d.joshi+29@gmail.com");
driver.findElement(webdriver.By.name("password")).sendKeys("123");
driver.findElement(webdriver.By.className("client-onboarding-signin-btn")).click().then(function(){
driver.findElement(By.css(".has-error")).getText().then(function (text) {
console.log(text);
try{
expect(text).to.be.a("Invalid email or password");
done();
} catch (e) {
done(e);
}
});
});
});
});
根据我的理解,这个测试用例应该通过,因为我期待无效的用户名和密码并且得到了相同的。但是,它会抛出断言错误。那是, 1) 使用不正确的凭据登录登录: AssertionError:预期 'Invalid email or password' 是无效的电子邮件或密码
您使用了错误的断言。您要使用:
expect(text).to.equal("Invalid email or password");
.to.be.a
(实际上只是对名为 a
的断言的调用)断言该值具有特定的 类型 。这是一些说明差异的代码:
const expect = require("chai").expect;
it("a", () => expect("foo").to.be.a("foo"));
it("equal", () => expect("foo").to.equal("foo"));
it("correct use of a", () => expect("foo").to.be.a("string"));
第一个测试将失败。第二个和第三个是正确的用法,所以通过了。
您可以找到 a
here 的文档。