Unable to identify whether my test case pass or fail

前端 未结 1 1368
梦谈多话
梦谈多话 2021-01-28 05:36

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

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

1)

1条回答
  •  一生所求
    2021-01-28 06:13

    Two things:

    1. 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

    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
    
      });
    });
    

    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!

    0 讨论(0)
提交回复
热议问题