angular.mock.module 和 angular.mock.inject 函数在 Jasmine 之前都不起作用



我想使用jasmine的beforeAll而不是beforeEach,但是angular.mock.module和angular.mock.inject函数在beforeAll中不起作用,而它们在beforeEach中工作。

这是我的测试。相同的代码在之前每种方法中都有效。

describe("This is a test", function () {
    beforeAll(module("app"));
    var vm;
    beforeAll(function() {
        angular.mock.module(function ($provide) {
            $provide.factory("dataService", ["$q", function ($q) {
                return {
                    getSomeDataById: function () { return $q.resolve({ }); }
                };
            }]);
        });
        angular.mock.inject(function (_$controller_,dataService) {
            vm = _$controller_("TestController",
            {
                dataService: dataService
            });
        });
    });
});

我遇到了类似的问题,使用 module.sharedInjector(( 调用为我解决了它:

describe("This is a test", function () {
    // Call SharedInjector before any call that might invoke the injector
    angular.mock.module.sharedInjector();
    beforeAll(module("app"));
    var vm;
    beforeAll(function() {
        angular.mock.module(function ($provide) {
            $provide.factory("dataService", ["$q", function ($q) {
                return {
                   getSomeDataById: function () { return $q.resolve({ }); }
               };
            }]);
        });
        angular.mock.inject(function (_$controller_,dataService) {
            vm = _$controller_("TestController",
            {
                dataService: dataService
            });
        });
    });
});

链接文档页面中的快速片段来解释为什么有效(强调我的(:

[ angular.mock.module.sharedInjector ] 确保单个注入器将用于给定描述上下文中的所有测试。这与每个测试用例创建新注入器的默认行为形成鲜明对比。

当您想利用Jasmine的beforeAll((或mocha的before((方法时,请使用sharedInjector。在设置任何其他将创建(即调用模块(((或使用(即调用 inject(((注入器的钩子之前调用 module.sharedInjector((。

最新更新