简体   繁体   English

angular.mock.module 和 angular.mock.inject 函数在 jasmine 的 beforeAll 中不起作用

[英]angular.mock.module and angular.mock.inject functions are not working in jasmine's beforeAll

I want to use jasmine's beforeAll instead of beforeEach but angular.mock.module and angular.mock.inject functions are not working in beforeAll whereas they're working in beforeEach.我想使用茉莉花的 beforeAll 而不是 beforeEach 但 angular.mock.module 和 angular.mock.inject 函数在 beforeAll 中不起作用,而它们在 beforeEach 中工作。

Here is my test.这是我的测试。 the same code is working in beforeEach approach.相同的代码在 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
            });
        });
    });
});

I was facing a similar problem and using the module.sharedInjector() call resolved it for me:我遇到了类似的问题,使用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
            });
        });
    });
});

A quick snippet from the linked docs page to explain why that works (emphasis mine): 链接文档页面中的一个快速片段,用于解释为什么有效(强调我的):

[ angular.mock.module.sharedInjector ] ensures a single injector will be used for all tests in a given describe context. [ angular.mock.module.sharedInjector ] 确保单个注入器将用于给定描述上下文中的所有测试。 This contrasts with the default behaviour where a new injector is created per test case.这与每个测试用例创建新注入器的默认行为形成对比。

Use sharedInjector when you want to take advantage of Jasmine's beforeAll() , or mocha's before() methods.当您想利用 Jasmine 的 beforeAll()或 mocha 的 before() 方法时,请使用 sharedInjector Call module.sharedInjector() before you setup any other hooks that will create (ie call module()) or use (ie call inject()) the injector.在设置将创建(即调用module())或使用(即调用inject())注入器的任何其他钩子之前调用module.sharedInjector()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么$仅在'angular.mock.module'函数中提供,$ q仅在'angular.mock.inject'函数中可用? - Why is $provide only available in the 'angular.mock.module' function, and $q only available in the 'angular.mock.inject' function? Angular Jasmine,angular.mock.inject和inject有什么区别? - Angular Jasmine, What is the difference between angular.mock.inject and inject? angular.mock.inject方法的问题 - Issue with angular.mock.inject method 使用angular-mocks.js时,使用Jasmine的AngularJS测试无法调用“angular.mock.module” - AngularJS testing with Jasmine unable to call “angular.mock.module” while using angular-mocks.js 在Karma + Jasmine下使用angular.mock.inject遇到TypeScript AngularJS控制器测试问题 - TypeScript AngularJS controller test issue with angular.mock.inject under Karma + Jasmine angular.mock.inject()方法什么时候立即执行? - When does the angular.mock.inject() method execute immediately? 将Angular模拟数据模块传递或注入茉莉花测试? - Pass or inject Angular mock data module into jasmine tests? 在业力中使用angular.mock.inject给出“ TypeError:无法读取null的属性'$ injector'” - Using angular.mock.inject in karma gives “TypeError: Cannot read property '$injector' of null” 模拟依赖时,angular.mock.module函数如何知道要寻址的模块? - How does the angular.mock.module function know what module to address when mocking dependencies? 角+茉莉:模拟指令 - angular + jasmine: mock a directive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM