测试适用于浏览器但不适用于PhantomJS

时间:2015-09-03 15:25:53

标签: javascript phantomjs

我对一个Ajax请求进行了Mocha(Chai?)测试,该测试在将HTML文件用作测试运行器时有效,但在从PhantomJS运行时则无效。

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

我的问题:

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

Karma报道:

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

2 个答案:

答案 0 :(得分:2)

在Phantom 1.x中没有Function.prototype.bind,它只在2.0+中实现。这是一个常见问题。升级或使用polyfill应解决此问题。

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

答案 1 :(得分:1)

我通过将PhantomJS从1.9.7升级到2.0并进行一些环境更改来解决这个问题。

使用public class GenericRepository<TEntity> : IRepository<TEntity>, IDisposable where TEntity : class { internal BackendContainer context; internal DbSet<TEntity> dbSet; public GenericRepository(BackendContainer context) { this.context = context; this.dbSet = context.Set<TEntity>(); } public GenericRepository() : this(new BackendContainer()) { } public virtual IQueryable<TEntity> All() { return dbSet.AsQueryable(); } } 安装PhantomJS 2.0(在OSX上)。

(在撰写本文时,没有用于升级PhantomJS的apt-get或npm路径。我正在使用Linux进行构建:http://phantomjs.org/build.html。)

请注意,即使在此升级之后,sudo npm -g install phantomjs2仍在运行Phantom 1.9.8。我用npm test解决了这个问题(这会随着实际安装了phantomjs而变化)。

这是一个黑客解决方案 - 我不知道为什么PhantomJS 2.0在1.9.8没有时工作,也不知道为什么必须导出PHANTOMJS_BIN。但它确实有效。