简体   繁体   English

如何将$ rootScope注入AngularJS单元测试?

[英]How do I inject $rootScope into an AngularJS unit test?

Suppose I have a service that depends on a value in $rootScope, as with the following (trivial) service: 假设我有一个服务依赖于$ rootScope中的值,就像下面的(普通)服务一样:

angular.module('myServices', [])
.factory('rootValGetterService', function($rootScope) {
    return {
        getVal: function () {
            return $rootScope.specialValue;
        }
    };
});

If I want to unit test this by putting a value in $rootScope, what is the best way to go about it? 如果我想通过在$ rootScope中添加一个值进行单元测试,那么最好的方法是什么?

...
var $rootScope;
beforeEach(inject(function(_$rootScope_) {
  $rootScope = _$rootScope_;
}));
...

By using provide(), you can inject a new $rootScope: 通过使用provide(),您可以注入一个新的$ rootScope:

describe('in rootValGetter', inject(function ($rootScope) {
    var scope;
    var testRootValGetter;

    beforeEach(function () {

        scope = $rootScope.$new();

        module(function ($provide) {
            $provide.value('$rootScope', scope);
        });

        inject(function ($injector) {
            testRootValGetterService = $injector.get('rootValGetterService');
        });
    });

    it('getVal returns the value from $rootScope', function() {
        var value = 12345;

        scope.specialValue = value;

        expect(testRootValGetterService.getVal()).toBe(value);
    }
}

包括angular-mocks.js ,然后使用angular.mock.inject

Just try to give a more detailed answer including the test case: 试着给出更详细的答案,包括测试用例:

... ...

var $rootScope;
beforeEach(inject(function(_$rootScope_) {
  $rootScope = _$rootScope_;
}));

... ...

  it('getVal returns the value from $rootScope', function() {
        var value = 12345;
        $rootScope.specialValue = value;
        expect(testRootValGetterService.getVal()).toBe(value);
    }

Here's what I did: 这是我做的:

it('some kind of wacky test', function($rootScope, Translate){
    $rootScope.lang = 'en';
    expect(Translate('overview').toBe('Overview');
}

Hope this helps others as this was the solution to a similar problem. 希望这有助于其他人,因为这是解决类似问题的方法。

var rootValGetterService;

beforeEach(inject(function($rootScope,$injector) {
    $rootScope.specialValue = "test";
    rootValGetterService= $injector.get('rootValGetterService');
}));

it("Should have a service", function () {
    expect(rootValGetterService).toBeDefined();
});

Instead of creating a new scope as you would if you were injecting $scope you can mock the properties you need directly into $rootScope . 您可以直接在$rootScope模拟所需的属性,而不是像注入$scope scope那样创建新的范围。

Then $rootScope will be injected with those properties available in the code you are testing. 然后$rootScope将注入您正在测试的代码中可用的那些属性。

At least this is the way I solved the same problem. 至少这是我解决同样问题的方式。

The following code should work in your example. 以下代码应该适用于您的示例。

beforeEach(inject(function($rootScope) {
    $rootScope.specialValue = 'whatever';
}));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM