用业力测试异步

时间:2014-11-25 21:24:06

标签: karma-runner karma-jasmine

我正在尝试使用karma和jasmine设置一些异步测试。我显然犯了一个非常愚蠢的错误,但我需要它向我指出。在尽可能简化之后,我有以下内容:

的package.json

{
  "name": "newtest",
  "version": "0.0.0",
  "scripts": {
    "test": "karma start karma.conf.js"
  },
  "devDependencies": {
    "karma": "^0.12.28",
    "karma-chrome-launcher": "^0.1.5",
    "karma-jasmine": "^0.2"
  }
}

karma.conf.js

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: [
      'tests/**/*.js'
    ],
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: false,
    browsers: ['Chrome'],
    singleRun: true
  });
};

测试/ dummy.spec.js

describe("Testing async", function() {
  it('should fail this test', function(done) {
    setTimeout(function(){
      expect(1).toBe(2);
      done();
    }, 1000);
  });
  it('should not fail this test', function(done) {
    done();
  });
});

我得到以下内容:

npm test
> newtest@0.0.0 test /home/mark/Projects/newtest
> karma start karma.conf.js
INFO [karma]: Karma v0.12.16 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 39.0.2171 (Linux)]: Connected on socket T7j6LvNAwvS89wUdymCb with id 16891024
Chrome 39.0.2171 (Linux) Testing async should not fail this test FAILED
    TypeError: undefined is not a function
        at null.<anonymous> (/home/mark/Projects/newtest/tests/dummy.spec.js:12:5)
Chrome 39.0.2171 (Linux): Executed 2 of 2 (1 FAILED) (0.007 secs / 0.005 secs)
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

所以我认为应该失败的测试是通过罚款,反之亦然。有人能指出我的错误吗?

2 个答案:

答案 0 :(得分:0)

我猜第一个失败是因为当达到超时时,测试已经完成,所以两者都不起作用。

你没有使用茉莉花2。

语法对我来说似乎很好,我可以告诉你的是我的配置(工作正常)的区别:

我已经将karma-jasmine放在依赖关系中了〜:

"dependencies": {
...
"karma-jasmine": "~0.2.0"
},

我正在使用PhantomJS:

browsers:['PhantomJS'],

答案 1 :(得分:0)

我不知道为什么会遇到这个问题,但我尝试过另一台机器并按预期工作。