angularjs – 创建单元测试,其中每个测试都有其特定的测试数据和
发布时间:2020-12-17 16:58:04  所属栏目:安全  来源:网络整理 
            导读:我有这个测试结构并且它正在工作但是当我在这个单元测试.js文件中放入更多测试时它不能满足我的要求,但是所有相关测试都应该去那里. 工作方式: 'use strict';describe('lessonplannerFactory unit tests',function () { // load the service's module // be
                
                
                
            | 
                         
 我有这个测试结构并且它正在工作但是当我在这个单元测试.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,1)),moment(new Date(2014,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为时已晚,因为之前注入/创建了lessonPlannerFactory,并且lessonPlannerFactory将真正的存根对象作为依赖项.只是看看: '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,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,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方法中,而是在单元测试本身内部,因为我需要一个特定的存根! 解决方法
 帕斯卡,我仍然不明白为什么嵌套描述不适合你.也许我只是没有得到你的要求? 
  
  
  
        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
        });
    });
});
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
