TypeError: 'undefined' is not an object (evaluating 'currentSpec.queue.running')

≡放荡痞女 提交于 2019-12-05 07:42:04

问题


I am using karma + jasmine.

Now thatI have tried one way of mocking a dependent factory I get this error:

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
    };
});

The lessonplannerFactory uses the dateFactory so I have to mock it for a real unit test.

When this code is run in debugging:

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

Then the debugging stops because somewhere in the code happens the error posted at the top.

I know this link: Module not found error in AngularJS unit test

It has my error message, but I do not understand the solution to adapt it to my scenario.

Anyone can help please?


回答1:


I only have a few minutes right now but I think one of your issues here is that you are trying to test much more than a unit. Unit testing is very specific and simplistic in its purpose. When I do it I try to inject as little as possible and minimize dependencies. In your case, instead of having one test suite for this I think you should have a suite for each one. Try something more like this:

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);
        }));
    });
});

I hope that helps out a bit.

Jordan




回答2:


This one solves my issue:

try to change isSpecRunning function on angular-mocks.js

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


来源:https://stackoverflow.com/questions/23480873/typeerror-undefined-is-not-an-object-evaluating-currentspec-queue-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!