【问题标题】:Testing jQyery.get with mocha.js does not work, nor do other Deferred (RSVP)用 mocha.js 测试 jQuery.get 不起作用,其他 Deferred (RSVP) 也不起作用
【发布时间】:2015-12-14 00:57:06
【问题描述】:

我正在尝试使用 mocha.js 运行一些 JavaScript 测试。到目前为止一切都很好,但是我在测试任何关于 jQuery 请求或其他承诺/延迟的东西时遇到了很大的问题。

我的代码在浏览器中运行良好,之前大量使用 $.Deferred,但使用 Mocha 我无法运行。

我尝试了不同的编写方式,还切换到了一个“合适的”Promise 库,因为众所周知 jQuery 会对其实现造成麻烦。

正确的 RSVP.Promises 工作正常,RSVP.Defer 仍然没有,而且我无法让任何涉及 jQuery 的工作(比如发出请求)。

我确实找到了一些建议,将 jQuery 包装到正确的 Promise 中应该可以完成这项工作,但找不到任何实际这样做的方法,这不会失败。

这是一个显示问题的测试用例:

'use strict';

var RSVP = require('rsvp');
var    $ = require('jquery');

describe('promises', function() {

  it('works with RSVP.Promise', function(done) {
    new RSVP.Promise(function(resolve, reject) {
      resolve();
    }).then(function() {
      done();
    });
  });

  it('works with RSVP.Deferred promise', function(done) {
    RSVP.defer().done(function() {
      done();
    }).resolve();
  });

  it('works with $.Deferred', function(done) {
    $.Deferred().done(function() {
      done();
    }).resolve();
  });

  it('works with $.get (promise syntax)', function(done) {
    $.get('http://google.com', function() {
      done();
    });
  });

  it('works with $.get (defered syntax)', function(done) {
    $.get('http://google.com').done(function() {
      done();
    });
  });

});

运行:

$ mocha --compilers jsx:babel/register

给我以下输出:

promises
    ✓ works with RSVP.Promise
    1) works with RSVP.Deferred promise
    2) works with $.Deferred
    3) works with $.get (promise syntax)
    4) works with $.get (defered syntax)


  1 passing (59ms)
  4 failing

  1) promises works with RSVP.Deferred promise:
     TypeError: undefined is not a function
      at Context.<anonymous> (test/test.js:17:26)

  2) promises works with $.Deferred:
     TypeError: undefined is not a function
      at Context.<anonymous> (test/test.js:23:21)

  3) promises works with $.get (promise syntax):
     TypeError: undefined is not a function
      at Context.<anonymous> (test/test.js:31:7)

  4) promises works with $.get (defered syntax):
     TypeError: undefined is not a function
      at Context.<anonymous> (test/test.js:37:7)

只要不涉及 jQuery Promise/Defer,其他测试就可以完美运行。

有没有比放弃 jQuery 来发出请求更好的解决方案,还是我做错了什么?测试的目的是测试实际的请求/响应,因此这里也没有选择模拟请求。

就像我说的,无论是否使用 Promise/Defer jQuery,代码都可以在浏览器中完美运行。所以我猜这是Test runner或它运行的node.js环境(?)的问题。

【问题讨论】:

    标签: javascript jquery node.js promise mocha.js


    【解决方案1】:

    要测试 HTTP 请求,您可以使用:https://github.com/visionmedia/supertest

    这很好,或者你可以使用 jQuery 来发出 HTTP 请求:https://github.com/visionmedia/superagent

    我认为它在 nodeJS 中比使用 jQuery 更标准。

    【讨论】:

    • 确实,您应该在测试时始终模拟 HTTP 请求。您不是在测试是否可以发出请求,而是在测试请求处理程序产生预期结果
    • 如果你想做功能/集成测试,你甚至可以提出请求,看看整个过程是否有效。
    • 是的,你说的很对。我应该更清楚地说“单元测试”。
    • 你是对的,在单元测试中你应该模拟出请求来只测试自己的行为。在我的情况下,我确实想要提出请求,因为我的测试用例实际上是在检查我的代码是否与请求的结果相抵触(因为结果可能会随着时间而改变)。
    • 而主要目标是让代码在浏览器中运行。我不确定运行测试是否应该定义浏览器代码的编写方式(使用为节点而不是浏览器制作的库)。 Superagent 似乎也可以在浏览器中工作(但似乎缺乏对旧版本的支持),所以我可能会尝试一下。
    猜你喜欢
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    相关资源
    最近更新 更多