【问题标题】:Mocha test doesn't call .then from function callMocha 测试不调用 .then 从函数调用
【发布时间】:2016-02-29 20:41:56
【问题描述】:

我一遍又一遍地测试了这个,试图找出一个解决方案,但不能。我已经尝试用谷歌搜索所有内容,但找不到任何产生任何结果的内容。我只使用 mocha 模块。问题是,当我运行我的测试时,它根本不会在我的函数中调用 .then 语句。我已经使用 console.log(); 测试了测试脚本之外的方法;他们按要求执行。

这是两个 cmets 的测试。

it('Should pass if the currently set frame name is top_page', function () {

    chromeDriver.get('http://www.site_with_frame.com');

    frameHandler.switchToFrame('top_page');

    frameHandler.getCurrentFrameName(function (name) {
        console.log(name); //This is never called.
        done(); //Should this be here?
    });
});

这是我用回调调用的函数

this.getCurrentFrameName = function(callback) {

    //This is called once in test
    console.log('getCurrentFrameName function '); 

    driver.executeScript('return self.name').then(function(name) 
    {
        //This is never called in test
        console.log('getCurrentFrameName .then '); 
        return callback(name);
    });
};

【问题讨论】:

  • 您需要在顶级函数中传递 done(即 it(..., function(done) { - 检查 mocha 文档的异步代码部分:mochajs.org - 是的,您的done 调用在正确的位置。
  • @gabdallah 我已经按照你所说的添加完成,我收到错误错误:超过 2000 毫秒的超时。确保在此测试中调用了 done() 回调。在 null. (C:\Users\charles.sexton\WebstormProjects\JS-Selenium-Toolkit\node_modules\mocha\lib\runnable.js:215:19)
  • 您需要编辑此问题以显示minimal reproducible example。如果我使用您的代码,它不起作用,但失败的方式与您在问题中显示的不同。因此,为了避免人们只是猜测可能的问题,请将问题设为minimal reproducible example

标签: javascript mocha.js


【解决方案1】:

我正在回答我自己的问题。我必须将 done 作为参数包含在内,然后指定超时和 done 函数调用。

describe('FrameHandler', function () {
   it('Should pass if the currently set frame name is top_page', function (done) {
      this.timeout(15000); // Include this
      chromeDriver.get('http://www.site_with_frame.com');

      frameHandler.switchToFrame('top_page');

      frameHandler.getCurrentFrameName(function (name) {
      console.log(name); //This is never called.
      done(); //Include this
      });
   });
}

【讨论】:

  • 您在it('Should pass if the currently set frame name is top_page', function (done) { 中似乎缺少done
猜你喜欢
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-26
  • 1970-01-01
  • 2019-07-26
  • 2016-12-24
相关资源
最近更新 更多