سؤال

I have this test structure and it is working but It does not cover my requirements when I put more tests in this unit test .js file , but all related tests should go there.

WORKING:

'use strict';
describe('lessonplannerFactory unit tests', function () {

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

    // 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
            var days = [ moment(new Date(2014, 0, 1)), moment(new Date(2014, 0, 2))];
            return days;
        }
    };

    // load the service's module
    beforeEach(module('clientApp', function ($provide) {
        $provide.value('dateFactory', dateFactory);
    }));

    // Arrange
    var lessonPlannerFactory;
    beforeEach(inject(function (_lessonPlannerFactory_) {  // The underscores before/after the factory are removed by the $injector
        lessonPlannerFactory = _lessonPlannerFactory_;
    }));

    it('creates periods by a daily rotation of n according to a given timetable for a certain timespan', inject(function (_dailyPeriodsTestDataFactory_) {

        // Arrange
        var testCases = _dailyPeriodsTestDataFactory_.testCases;  // specific test data

        // Act
        // Do something with the injected lessonPlannerFactory.method1(testCases[0],..);

        // Assert

    }));
});

What I want is this described in my own words or pseudo code:

describe("lesson planner factory unit tests")
{

   // Inject a lesson planner factory instance for each test

test1(_specificTestData1_)
{
    // Arrange
    var data = _specificTestData1_;

    var stub1 = { } // setup specific stub

    inject this specific stub into this test with $provide.value = mock // This stub is used INSIDE the method1 to stub out a factory dependency

    // Act
    var result = lessonPlannerFactory.method1(data,..);

    // Assert

}

test2(_specificTestData2_)
{
    // Arrange
    var data = _specificTestData2_;

    var stub2 = { } // setup specific stub

    inject this specific stub into this test with $provide.value = mock // This stub is used INSIDE the method2 to stub out a factory dependency

    // Act
    var result = lessonPlannerFactory.method2(data,..);

    // Assert

}

}

And this is what I have tried and it does not work.

I assume that assinging the stub to the $provide.value is too late at this point inside the test because the lessonPlannerFactory is injected/created before and the lessonPlannerFactory has the real stub object as dependency. Just look:

'use strict';
describe('lessonplannerFactory unit tests', function () {

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

    // Arrange
    var lessonPlannerFactory;
    beforeEach(inject(function (_lessonPlannerFactory_) {  // The underscores before/after the factory are removed by the $injector
        lessonPlannerFactory = _lessonPlannerFactory_;
    }));

    it('creates periods by a daily rotation of n according to a given timetable for a certain timespan', inject(function (_dailyPeriodsTestDataFactory_) {

        // Arrange
        var testCases = _dailyPeriodsTestDataFactory_.testCases;  // specific test data

        // specific test stub
        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
                var days = [ moment(new Date(2014, 0, 1)), moment(new Date(2014, 0, 2))];
                return days;
            }
        };

        // load the stub
        module('clientApp', function ($provide) {
            $provide.value('dateFactory', dateFactory);
        });

        // Act          on the testcases in a for loop
        var result = lessonPlannerFactory.createPeriodsDaily(testCases[i].data1,..);

        // Assert
    }));
});

Thats the error I get:

TypeError: Cannot read property 'running' of undefined
    at isSpecRunning (http://localhost:3000/base/app/bower_components/angular-mocks/angular-mocks.js:1924:65)

So how can I inject a stub for a service dependency NOT in the beforeEach method but INSIDE the unit test itself, because I need a specific stub!?

هل كانت مفيدة؟

المحلول

Pascal, I still don't understand why nested describes won't work for you. Maybe I just don't get your requirements?

describe('overall test of A', function() {
    describe('first test of A', function() {
        beforeEach(function() {
            // arrange
        });

        it('should do X', function() {
            // test
            // assert
        });
    });
    describe('second test of A', function() {
        beforeEach(function() {
            // arrange
        });

        it('should do Y', function() {
            // test
            // assert
        });
    });
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top