简体   繁体   English

在mocha.js中使用Empty函数

[英]Using Empty function in mocha.js

Am new to Javascript ,node.js and mocha. 是Javascript,node.js和mocha的新功能。 as i was looking to basic example i encountered an empty function "function()". 正如我在寻找基本示例一样,我遇到了一个空函数“ function()”。 What's the ppurpose of using empty function. 使用空函数的目的是什么?

var assert = require('assert'),
var test = require('selenium-webdriver/testing'),
var webdriver = require('selenium-webdriver');

test.describe('Google Search', function() {
  test.it('should work', function() {
    var driver = new webdriver.Builder().
    withCapabilities(webdriver.Capabilities.chrome()).
    build();

    driver.get('http://www.google.com');
    var searchBox = driver.findElement(webdriver.By.name('q'));
    searchBox.sendKeys('simple programmer');
    searchBox.getAttribute('value').then(function(value) {
      assert.equal(value, 'simple programmer');
    });
    driver.quit();
    done();
  });
});

when i run this sample code i got below error , can u please help me resolving this issue please . 当我运行此示例代码时,出现以下错误,请您帮我解决这个问题。

error : Error: timeout of 2000ms exceeded. 错误:错误:超时超过2000ms。 Ensure the done() callback is being called in this test. 确保此测试中调用了done()回调。

as i see am able to launch browser and open google.com as writted in program but i see fail still 我看到能够启动浏览器并按照程序中的说明打开google.com,但我仍然看到失败

Google Search 1) should work Google搜索1)应该可以工作

0 passing (2s) 1 failing 0通过(2s)1失败

1) Google Search should work: Error: timeout of 2000ms exceeded. 1)Google搜索应该可以正常工作:错误:超过2000毫秒的超时时间。 Ensure the done() callback is being called in this test. 确保此测试中调用了done()回调。 at null. 为空。 (C:\\Users\\kashyap\\AppData\\Roaming\\npm\\node_modules\\mocha\\lib\\runnable.js:170:19) at Timer.listOnTimeout (timers.js:110:15) (C:\\ Users \\ kashyap \\ AppData \\ Roaming \\ npm \\ node_modules \\ mocha \\ lib \\ runnable.js:170:19)在Timer.listOnTimeout(timers.js:110:15)

can you please also suggest me some links to know and try mocha+nodejs with selenium 您能否也建议我一些链接以了解和尝试使用硒的mocha + nodejs

Your function are not empty. 您的函数不为空。 In fact, in javascript, there are 2 ways to define a function : 实际上,在javascript中,有两种定义函数的方法:

function myFunction (arg1, arg2){
    //Do some stuff
}

or 要么

var myFunction = function(arg1, arg2){
    //Do some stuff
}

In both cases, the function is stored in the variable called "my function" (with different scopes, though). 在这两种情况下,函数都存储在名为“我的函数”的变量中(尽管作用域不同)。

So basically, in javascript, a function is a value. 因此,基本上,在javascript中,函数是一个值。 You may draw a parallel with functional pointers in c, to understand the way it works. 您可以在c中使用函数指针绘制一个平行线,以了解其工作方式。

Now, what mocha does, is getting the test process as a parameter of the function describe, and the test itself as a parameter of the function it (you can set initiliazing/cleaning processes with before/afer functions as well). 现在,mocha所做的就是将测试过程作为函数描述的参数,并将测试本身作为函数描述的参数(您也可以使用before / afer函数设置初始化/清理过程)。

We can now see what the "done" function is. 现在,我们可以看到“完成”功能是什么。 The done function (you can give it the name you want, done is just the most common name) is the function you should call when your test is over. 完成功能(您可以给它起所需的名称,完成只是最常用的名称)是测试结束时应调用的功能。 It's given as a parameter of the function that defines the process you're in. Quick example : 它作为定义您所处过程的函数的参数给出。快速示例:

it('should work', function(done){
    //do some testing
    done();
});

does the same thing as : 与...具有相同的作用:

it('should work', function(){
    //do some testing
});

But where that parameter is useful is when you're testing asynchronous functions. 但是该参数在测试异步函数时有用。 For example, I want to test a query in a database, which is not instant. 例如,我要在数据库中测试查询,这不是即时的。

it("should work", function(){
    dbDriver.get('key1', function(data){
        assert(data == goodValue);
    });
});

The previous example will not work properly, cause the db request is not instantaneous and is an asynchronous call, which mean that my test will keep running, and my assertion will happen after we left the test section. 前面的示例将无法正常工作,因为db请求不是瞬时的并且是异步调用,这意味着我的测试将继续运行,并且断言将在我们离开测试部分之后发生。 So mocha will think that everything was ok, and then will ignore my assertion afterwards. 因此,摩卡咖啡会以为一切都很好,然后会忽略我的主张。

To allow asynchronous function testing, mocha designed the done parameter, which makes the test to keep on running while the parameter function is not called, so, if I want my test to run correctly, I have to do it like this : 为了允许异步功能测试,mocha设计了done参数,该参数使测试在不调用参数函数的情况下继续运行,因此,如果我希望我的测试正确运行,则必须这样做:

it("should work", function(done){
    dbDriver.get('key1', function(data){
        assert(data == goodValue);
        done();
    });
});

Now, mocha will be stuck in this test while the done function is not called. 现在,在不调用done函数的情况下,mocha将停留在此测试中。 And, it will be called once the callback of the get function is called and the assertion is made. 并且,一旦调用get函数的回调并进行断言,它将被调用。

Now, let's look at the code. 现在,让我们看一下代码。 First of all, your call to done is uncalled (...) for. 首先,您的完成呼叫未被调用(...)。 Done is not given as a parameter of any of your callbacks, so it should be undefined, and, if that line was executed, you would simply get a syntax error. Done没有作为任何回调的参数给出,因此它应该是未定义的,并且,如果执行了该行,则只会出现语法错误。

Now, basically, what's happening in your code is that a function doesn't respond fast enough, so mocha considers that the test has timed out (default value is 2000 ms). 现在,基本上,您的代码中发生的是一个函数的响应速度不够快,因此mocha认为测试已超时(默认值为2000 ms)。

Basically, you should start testing adding the function one by one in your process to check which one is timing out, to then be able to reduce your problem to the timed out function only. 基本上,您应该开始在过程中一一测试添加功能,以检查哪个功能超时,然后才能将问题减少到仅超时功能。

If you want to learn mocha, the official website of the framework is pretty good : http://mochajs.org/ , and presents some good practice (like using before and after). 如果您想学习mocha,则该框架的官方网站非常不错: http : //mochajs.org/ ,并提供了一些很好的实践(例如,在前后使用)。

I hope I wasn't too long and that I could help you understanding mocha and javascript better. 希望我不太长,希望可以帮助您更好地了解Mocha和javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM