用摩卡模拟单元测试

时间:2012-12-06 19:17:38

标签: unit-testing mocking angularjs mocha karma-runner

我正在尝试设置一个简单的模拟示例,取自此github issue

'use strict';

var expect = chai.expect;

var app = angular.module('plunker', [])
    .factory('car', function(engine) {
        return {
            drive : function() {
                return 'Driving: ' + engine.speed();
            }
        }
    })
    .value('engine', {
        speed : function() {
            return 'fast';
        }
    });

describe('Testing a car', function() {
    var testEngine;

    beforeEach(function(){
        testEngine = {};
        angular.module('test', ['plunker']).value('engine', testEngine);
        module('test');
    });

    it('should drive slow with a slow engine', inject(function(car) {
        testEngine.speed = function() {
            return 'slow';
        };
        expect(car.drive()).to.equal('Driving: slow');
    }));
});

然而,当我尝试使用Testacular(使用Mocha)运行它时,我收到以下错误:

info (watcher): Changed file "/path/to/testMock.js".
PhantomJS 1.7 (Mac) Testing a car "before each" hook FAILED
    TypeError: 'undefined' is not an object (evaluating 'currentSpec.queue.running')
        at isSpecRunning (/path/to/angular-mocks.js:1626)
        at /path/to/angular-mocks.js:1648
        at /path/to/testMock.js:25
PhantomJS 1.7 (Mac): Executed 29 of 41 (1 FAILED) (0.215 secs / NaN secs)

我做错了什么?

0 个答案:

没有答案