[英]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.