0

我写了以下html文件:

<!DOCTYPE html> <html lang="en"> <head>
    <meta charset="UTF-8">
    <title></title> </head> <body>
    <h1> HELLO WORLD </h1>
    <button type="button"> CLICK HERE</button>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        $('button').click(function() {
            $('h1').text("Thank you");
            var ans = prompt("Please type your age");
            if (ans > 80) {
                $('h1').text("You're old");
            }
        })
    </script> </body> </html>

我想测试一下,一旦用户单击按钮并给出年龄超过 80 岁,页面就会按预期运行,并且 h1-tag 文本会更改为“你老了”。

我的问题是如何编写 Jasmine 规范来测试此功能?

4

1 回答 1

1

您需要使用“间谍”对象来拦截“提示”功能并返回您自己的值。检查标题文字后。代码可能看起来像...

deascribe("test inline function:", function() {
  it("caption should be 'too old' for age of more than 80", function () {
    spyOn(window, 'prompt').and.returnValue(81);
    $('button').click();
    expect($('h1').text()).toBe("You're old");
  });
});
于 2017-01-27T18:42:47.083 回答