javascript - 测试在浏览器中有效,但在 PhantomJS 中无效

标签 javascript phantomjs

我有一个 Ajax 请求的 Mocha(Chai?)测试,它在使用 HTML 文件作为测试运行程序时有效,但在从 PhantomJS 运行时无效。

Karma 正在成功运行其他测试。

我的问题:

为什么 Karma 会失败?
如何让这个测试在 PhantomJS 中运行?

业力报告:

PhantomJS 1.9.8 (Linux 0.0.0) MyAPI "before each" hook for "should parse fetched data as JSON" FAILED
    TypeError: 'undefined' is not a function (evaluating 'function(xhr) {
          this.requests.push(xhr);
        }.bind(this)')
        at [...]ajax_test_demo.test.js:22

测试文件:

chai.should();

describe('MyAPI', function() {

  // var bike_app = new BikeApp();

  beforeEach(function() {
    this.xhr = sinon.useFakeXMLHttpRequest();

    this.requests = [];
    this.xhr.onCreate = function(xhr) {
      this.requests.push(xhr);
    }.bind(this);
  });

  afterEach(function() {
    this.xhr.restore();
  });

  it('should parse fetched data as JSON', function(done) {
      var data = { foo: 'bar' };
      var dataJson = JSON.stringify(data);

      myapi.get(function(err, result) {
          result.should.deep.equal(data);
          done();
      });

      this.requests[0].respond(200, { 'Content-Type': 'text/json' }, dataJson);
  });

});

想法

Karma 似乎正在运行 Sinon,在调用 sinon.useFakeXMLHttpRequest 时没有提示,但该调用返回的对象缺少 onCreate 方法。唉,我的 JavaScript 很弱,我不知道如何调试它。

HTML 测试运行器:

<!DOCTYPE html>
<html>
    <head>
        <title>Mocha Tests</title>
        <link rel="stylesheet" href="node_modules/mocha/mocha.css">
    </head>
    <body>
        <div id="mocha"></div>
        <script src="node_modules/mocha/mocha.js"></script>
        <!-- edit from source file -->
        <script src="node_modules/sinon/pkg/sinon.js"></script>
        <script src="node_modules/chai/chai.js"></script>
        <script>mocha.setup('bdd')</script>
        <script src="lib/myapi.js"></script>
        <script src="test/ajax_test_demo.test.js"></script>
        <script>
            mocha.run();
        </script>
    </body>
</html>

Karma 配置:

'use strict';

var appConfig = require('./config');

module.exports = function(config) {
  config.set({

    basePath: '',

    frameworks: [
        'sinon',
        'mocha', 
        'chai', 
        'fixture',
    ],

  files: [
      'lib/*.js',
      'test/*.js',
      'test/*.html'
    ],

    preprocessors: appConfig.karma.preprocessors,    
    reporters: appConfig.karma.reporters,
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: appConfig.karma.autoWatch,
    browsers: appConfig.karma.browsers,
    singleRun: appConfig.karma.singleRun
  });
};

杂项

我正在使用本教程:https://www.airpair.com/javascript/posts/unit-testing-ajax-requests-with-mocha

最佳答案

Function.prototype.bind 在 Phantom 1.x 中不可用,它仅在 2.0+ 中实现。这是一个常见的问题。升级或使用 polyfill 应该可以解决这个问题。

https://github.com/ariya/phantomjs/issues/10522

关于javascript - 测试在浏览器中有效,但在 PhantomJS 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32379768/

相关文章:

pdf-generation - 让 PhantomJS 在渲染为 PDF 之前等待整个页面加载

rspec - 在针对 app_host 运行时,如何使用 Capybara 设置 Poltergeist 超时选项?

javascript - 将 PostGIS 点对象转换为 geoJSON 以进行制图

javascript - 类似于Google Visualization的图表工具

javascript - 全局(窗口)变量在 Emberjs 中行为异常

javascript - 使用 PhantomJS 下载网页

node.js - phantomjs - 加载共享库时出错 : libjpeg. so.8

javascript - 如何将网页上每个图像的大小加倍?

javascript - Mongo ISODate - 如何检查是否已过去 30 天或更长时间?

ember.js - 不正常 1 PhantomJS 在 CI 上运行 `ember test` 时意外退出