TypeError:'undefined'不是对象(评估'currentSpec.queue.running')

时间:2014-05-05 20:06:38

标签: angularjs jasmine

我正在使用业力+茉莉。

现在我尝试了一种模拟依赖工厂的方法,我得到了这个错误:

TypeError: 'undefined' is not an object (evaluating 'currentSpec.queue.running')
at C:/test/test/client/app/bower_components/angular-mocks/angular-mocks.js:1924
at C:/test/test/client/app/bower_components/angular-mocks/angular-mocks.js:1979
at C:/test/test/client/test/spec/services/lessonplannerfactory.js:31
at C:/test/test/client/node_modules/karma-jasmine/lib/boot.js:117
at C:/test/test/client/node_modules/karma-jasmine/lib/adapter.js:171
at http://localhost:3000/karma.js:189
at http://localhost:3000/context.html:85


// load the service's module
beforeEach(module('clientApp'));

var lessonPlannerFactory;
var data;

beforeEach(inject(function (_lessonPlannerFactory_, _dailyPeriodsTestDataFactory_) {  // The underscores before/after the factory are removed by the $injector
    lessonPlannerFactory = _lessonPlannerFactory_;
    data = _dailyPeriodsTestDataFactory_;

}));

beforeEach(function(){
    // Arrange
    var dateFactory = {
        getVisibleDateRange: function (startDate, endDate, visibleWeekDays) {
            // I do not care about the parameters, I just want to stub out the visible days with 2 stubs
            return [
                moment(new Date(2014,0,1)),
                moment(new Date(2014,0,2))
            ];
        }
    };

    module(function ($provide) {
        $provide.value('dateFactory', dateFactory);
    });
});

it('creates periods by daily rotation and a given timetable', function() {

    // Act
    var periods = lessonPlannerFactory.createPeriodsDaily(data.startDate, data.endDate, data.visibleDays, data.rotation, data.timetableEntries);

    // Assert

});

// LessonplannerFactory

angular.module('clientApp').factory('lessonPlannerFactory',['dateFactory', function (dateFactory) { // inject the dateFactory into this factory

    function createPeriodsDaily(startDate, endDate, visibleDays, weeklyRotation, timeTableEntries) {

        var visibleDateRange = dateFactory.getVisibleDateRange(startDate,endDate, visibleDays);

        // todo: Implement the whole algorythm
        var query = visibleDateRange;
        return query;
    }

    return {
        createPeriodsDaily: createPeriodsDaily

    };
}]);

// DateFactory

angular.module('clientApp').factory('dateFactory', function () {

    /// Computes all visible days with a given start date and end date (timespan)
    // startDate: momentJS object
    // endDate: momentJS object
    // visibleWeekDays: integer array of day index of a week
    function getVisibleDateRange(startDate, endDate, visibleWeekDays) {
        var visibleDateRange = [];

        // todo: Implement the algorythm

        return visibleDateRange;
    }

    return {
        getVisibleDateRange: getVisibleDateRange
    };
});

lessonplannerFactory使用了dateFactory,所以我必须模拟它才能进行真正的单元测试。

在调试中运行此代码时:

module(function ($provide) {
            $provide.value('dateFactory', dateFactory);
        });

然后调试停止,因为代码中的某处发生了错误发布在顶部。

我知道这个链接:Module not found error in AngularJS unit test

它有我的错误信息,但我不理解使其适应我的方案的解决方案。

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

我现在只有几分钟但我认为你的一个问题是你试图测试的不仅仅是一个单位。单元测试的目的非常具体和简单。当我这样做时,我尝试尽可能少地注入并最小化依赖性。在您的情况下,我认为您应该为每个测试套件配备一个套件,而不是为此设置一个测试套件。尝试更像这样的东西:

describe('DateFactory unit tests', function() {    
    beforeEach(module('clientApp'));


    describe("when getting a visible date range", function() {
        beforeEach(inject(function(dateFactory) {
            spyOn(dateFactory, 'getVisibleDateRange').andReturn(2013);
        }));

        it('should be 2013', inject(function(dateFactory) {
            expect(dateFactory.getVisibleDateRange(param1, param2, param3)).toBe(2013);
        }));
    });
});

describe('LessonPlannerFactory unit tests', function() {
    var data, lessonPlannerFactory;

    beforeEach(module('clientApp'));

    describe('When creating daily periods', function() {
        var query = "your visible date range";

        beforeEach(inject(function(lessonPlannerFactory) {
            spyOn(lessonPlannerFactory, 'createPeriodsDaily').andReturn(query);
        }));

        it('Should be the visible date range', inject(function(lessonPlannerFactory) {
            expect(lessonPlannerFactory.getVisibleDateRange()).toBe(query);
        }));
    });
});

我希望有所帮助。

约旦

答案 1 :(得分:0)

这个解决了我的问题:

尝试在angular-mocks.js上更改isSpecRunning函数

function isSpecRunning() {
    return currentSpec && currentSpec.queue && currentSpec.queue.running
        || currentSpec; // for Mocha.
}