无法调用函数来测试HTML事件

Unable to call the function, to test HTML Events

本文关键字:测试 HTML 事件 函数 调用      更新时间:2023-09-26

我需要在Mocha中测试HTML事件,我在Event.HTML文件中有以下代码

    <html>
    <head>
    <title>Html Attributes testing</title>
    <!-- Include CSS File Here -->
     <link rel="stylesheet" href="mocha.css" />

    <!-- Include JS File Here -->

     <script src="lib/jquery-1.11.2.js"></script>
     <script src="test-lib/chai.js"></script>
     <script src="test-lib/mocha.js"></script>
     <script> mocha.setup('bdd');</script>
     <script src="Test.js"></script>
     <script src="Application.js"></script>
        <script>
             window.onload = function() {
             mocha.run()
        };
        </script>
    </head>
    <body>
    <div id="mocha"></div>
    <p>This example uses Triggers to "click" event.</p>

    <script>
        $(document).ready(function(){
        $("#demo").trigger("click");
    });
    </script>

    <p id="demo" onclick="setTimeout(myFunction, 5000);">Click me.</p>

    </body>
    </html>
And the following code in Application.js file 
    function myFunction() {
        document.getElementById("demo").innerHTML = "YOU CLICKED ME!";
    }
And the following code in Test.js file
var expect = chai.expect;

describe('EventTesting', function() {
  it('Testing Events in JavaScript', function(done) {
  var v1 = myFunction();
  expect(v1).to.equal("YOU CLICKED ME!");
  done();
    });
});

在Application.js文件中,我使用document.getElementById("demo")调用了"<p id="demo">"属性,并将值设置为YOU CLICKED ME!,然后使用"触发器"单击它。

In Test.js file i want  to call that function which is reside inside "Application.js" file and need  to check whether its value is "YOU CLICKED ME!" and I want my testcase to be passed.

但我得到了以下错误:"AssertionError:预期undefined等于‘YOU CLICKED ME!’"

我做错了什么?

这实际上是你上次问这个问题的翻版。答案是一样的:

变量v1设置为函数myFunction()的返回值,但myFunction()[/em>不返回任何值。因此,当你试图将v1(其值将未设置,即"未定义")与"你点击了我!"进行比较时,你会得到错误。

我不会重复上面代码中所有不正确的地方,自上次以来,您几乎没有更改任何内容。关于你需要改变的所有事情的完整列表,请参阅上面链接中我对你最后一个问题的回答。