gpt4 book ai didi

javascript - Codeacademy 类(class)中的 Expect.js 验证问题

转载 作者:行者123 更新时间:2023-12-01 01:48:17 28 4
gpt4 key购买 nike

我正在 codeacademy 上创建一门类(class),使用户能够了解 HTML 表单的创建。这主要是为了我自己的娱乐,只是为了让我们都达成共识。读完submission test guidelines and API后我决定使用expect.js来处理我的大部分验证,因为我需要遍历 DOM。

因此,我正在创建的类(class)要求用户创建两个标签元素(标签标签内包含内容)和两个输入字段(带有 type 属性定义并将值设置为 text )。再次强调,标准如下:

  1. 创建两个 <label> <form> 内的元素元素。请务必指明用户将填写的内容!
  2. 创建两个 <input>每个 <label> 下面的元素元素。请务必包含并定义 type属性!

假设我有以下代码:

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form>
<label>Foo</label>
<input type="text">
<label>Bar</label>
<!-- should below here here -->
<input>
</form>
</body>
</html>

这一切都会过去的。根据我对 Expect.js 运行方式和当前验证的理解,它应该不会通过,因为用户忽略了添加第二个 type="text"属性

我的验证标准如下:

// do the items exist?
$expect('form > label').to.have.items(2, 'Did you forget to create two <label></label> tags?').and.to.have.text(/[a-zA-Z0-9]{1,}/i, 'Do the label tags have information inside of them? <label>My Label</label>');
$expect('form > input').to.have.items(2, 'Did you forget to create two <input> tags?').and.to.have.attr('type', 'text');

// do input fields exist below labels?
$expect('label').to.have.siblings('input', "Are the input fields below the labels?");

如果用户提交 HTML 代码,那么它应该会失败,但它会通过。我的断开连接是当用户输入 <label>第二组时和<input>标签。如果他们省略了第二个 <label> 内的内容和/或type值为 text 的属性还是会过去的。

我在这里缺少什么特别或独特的东西吗?我已经尝试了上面提供的带有和不带有方法链接的验证代码,但没有效果。

我尝试过的

  1. 在 Codeacademy 上查找有关如何纠正此问题的任何提示
  2. 查看了expect.js README以获取任何提示
  3. 在 StackOverflow 上查找任何提示
  4. 删除了方法链并重新建立了方法链

最佳答案

我认为您需要 that/which功能,这是有详细记录的。

$expect('form > input').to.have.items(2, 'Did you forget to create two <input> tags?').that.has.attr('type', 'text');

关于javascript - Codeacademy 类(class)中的 Expect.js 验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16445604/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com