【问题标题】:Create unit tests where each test has its specific test data and test mocks创建单元测试,其中每个测试都有其特定的测试数据和测试模拟
【发布时间】:2014-06-28 01:09:06
【问题描述】:

我有这个测试结构并且它正在工作,但是当我在这个单元测试 .js 文件中放置更多测试时它不能满足我的要求,但是所有相关的测试都应该去那里。

工作中:

'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

    }));
});

我想要的是用我自己的话或伪代码描述的:

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

}

}

这是我尝试过的,但它不起作用。

我假设在测试中此时将存根分配给 $provide.value 为时已晚,因为之前注入/创建了课程计划工厂,并且课程计划工厂具有真正的存根对象作为依赖项。看看吧:

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

这就是我得到的错误:

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

那么我怎样才能为服务依赖注入一个存根而不是在 beforeEach 方法中,而是在单元测试本身中,因为我需要一个特定的存根!?

【问题讨论】:

  • 如果需要,您可以嵌套描述并为每个它设置一个 beforeEach...
  • @aet 我知道有人会建议这样做,但是 beforeEach 对我的单元测试没有意义,因为每个测试都有一个它(...)并且每个测试都有其特定的测试数据和特定的嘲笑。我来自 C# 背景,很容易完成。如果 angularjs 无法做到这一点,我应该退出该框架并尝试使用 emberjs ...
  • 这不是角度问题,你不满意的是茉莉花。不知道为什么它“没有意义”——如果这是测试框架的工作方式,那为什么要反对它?
  • @aet 我不想与之抗争,我只是要求每个 it(..) 的特定模拟,而 beforeEach 只是不起作用。你知道比 jasmine 更灵活的 JS 测试框架吗?

标签: angularjs unit-testing jasmine angular-mock


【解决方案1】:

Pascal,我仍然不明白为什么嵌套描述对你不起作用。也许我只是不明白你的要求?

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

【讨论】:

  • 是的,这正是我写下来要做的,后来忙于其他事情。使用该解决方案,我只是不喜欢 beforeEach 的含义,因为只有一个,我希望有一个没有 Each 的 before 函数。我知道它很挑剔……但它具有误导性……
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
相关资源
最近更新 更多