无法确定我的测试用例是通过还是失败 [英] Unable to identify whether my test case pass or fail

查看:222
本文介绍了无法确定我的测试用例是通过还是失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Mocha点击link..etc来测试JavaScript中的事件,例如按钮点击。

I need to test "Events" in JavaScript like Button click, by clicking link..etc using "Mocha".

我有3个文件1)Login.html,2)Application.js,3)test.js。

I've 3 files 1) Login.html, 2) Application.js, 3) test.js.

1)Application.js文件包含以下代码:

1) Application.js file contain the following code :

function doclick() {

 document.getElementById("myBtn").innerHTML = alert("Going to test Events in JavaScript");
} 

2)test.js包含以下代码:

2) test.js contain the following code:

var expect = chai.expect;

describe('Testing', function() {
  it('Testing the Events in JavaScript', function() {

    var v1 = doclick();
    expect(v1).to.equal("Going to test Events in JavaScript");

  });

3)login.html包含以下代码:

3) login.html contain the following code:


document.getElementById(myBtn)。addEventListener(click,doclick);

document.getElementById("myBtn").addEventListener("click", doclick);

我需要测试单击按钮的时间,它应该显示警告,并显示此消息Go to to test Events in JavaScript实际上它确实如此。但是浏览器没有显示我的测试用例是通过还是失败。

I need to test when the button is clicked it should display the alert with this message "Going to test Events in JavaScript" actually it does. But the browser does not show whether my test case pass or fail.

我认为我的test.js文件出错了,我做错了什么?

I think I did mistake in my "test.js" file, What am I doing wrong?

推荐答案

两件事:


  1. doClick()

innerHTML值显示实际Button 上的,而不是单击按钮时显示的值。您无需执行或设置任何其他内容即可实现该功能;这就是你的addEventListener调用正在做的事情。如果你想显示提醒,所有doclick需要做的是:

The innerHTML value is what displays on the actual Button, not what runs when the button is clicked. You don't have to do or set anything else to get to that functionality; that's what your addEventListener call is doing. If you want to display an alert, all doclick needs to do is:

function doclick() {
    alert("Going to test Events in JavaScript");
}




  1. test.js

现在,您希望doclick不仅可以发送警报,还可以返回要在测试中使用的值。 JS。这需要是那个字符串吗?鉴于doclick只做了一件事,不是真的。如果您只想测试它运行,请将doclick返回true并最初将v1设置为false。然后将v1设置为期望值为true而不是长字符串。

Now, it looks like you want doclick to do more than just send the alert, and also return a value to be used in test.js. Does this need to be that string? Given that doclick only does one thing, Not really. If all you want to do is test that it ran, have doclick return true and set v1 to false initially. Then set your v1 to expect a value of true rather than that long string.

function doclick() {
    alert("Going to test Events in JavaScript");
    return true; //I ran, so return true.
}

var expect = chai.expect;

describe('Testing', function() {
  it('Testing the Events in JavaScript', function() {

    var v1 = false;
    v1 = doclick();
    expect(v1).to.equal(true); //or whatever the syntax here is, I haven't used expect before

  });
});

我假设你的语法的其余部分是正确的 - it()是有效的函数和你的test.js文件最终运行没有错误。如果对Mocha有更多了解的人想回答我可能会把他们的知识放在我的身上,但到目前为止还没有其他人回答它至少是解决一些JS代码相关问题的起点!

I'm assuming the rest of your syntax is correct - that "it()" is a valid function and that your test.js file does ultimately run without error. If someone with more knowledge of Mocha wants to answer I'd probably take their knowledge over mine, but given no one else has answered so far it's at least a starting point to address some of the JS code-related issues!

这篇关于无法确定我的测试用例是通过还是失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆