angularjs单元测试时使用$ rootScope。$新的() [英] angularjs unit test when to use $rootScope.$new()

查看:228
本文介绍了angularjs单元测试时使用$ rootScope。$新的()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白时,我应该使用$ rootScope =的 $ rootScope 的。新的$()在我的角度单元测试。这剪断你在很多单元测试的例子看。但在下面的例子中不起作用:

  angular.module(app.root,[])。工厂(rootFct功能($ rootScope){
    $ rootScope.amount = 12;
    返回{
        getAmount:功能(){
           返回($ rootScope.amount + 1);
        }
    }
});

相关联的单元测试不工作:

 描述('金额测试,函数(){
    VAR rootFct,$ rootScope;    beforeEach(函数(){
        angular.mock.module(app.root);
        angular.mock.inject(功能(_rootFct_,_ $ rootScope_){
            。$ rootScope = _ $ rootScope _ $新();
            rootFct = _rootFct_;
        });
    });    它('在rootScope 10集金额,函数(){
        VAR的结果= rootFct.getAmount();
        预期(结果).toBe(13);
        $ rootScope.amount = 15;
        结果= rootFct.getAmount();
        预期(结果).toBe(16);
    });
});

它只能当我改变了

  $ rootScope = _ $ rootScope _ $新();

  $ rootScope = _ $ rootScope_;

,所以我真的不明白何时使用新的$()和它有什么好?


解决方案

  创建一个新的范围出一个现有的时

$新()主要用于
  范围。


您code已经在服务注入rootScope所以它不会持有任何区别。

$新()可以,当你想从与父范围的某些属性可以使用/不带隔离。

这需要两个参数隔离和家长。
隔离(布尔值)需要是否需要从父范围隔离被使用。
家长(对象)明确定义父范围。

请注意,手动创建一个新的范围需要被破坏以及手动。

更多关于它在这里

I don't really understand when I should use the $rootScope = $rootScope.$new() in my angular unit tests. This snipped you see in many unit test examples. But in the following example that doesn't work:

angular.module("app.root", []).factory("rootFct", function ($rootScope) {
    $rootScope.amount = 12;
    return {
        getAmount: function () {
           return ($rootScope.amount + 1);
        }
    }
});

The associated unit Test doesn't work:

describe('Amount Tests', function() {
    var rootFct, $rootScope;

    beforeEach(function() {
        angular.mock.module("app.root");
        angular.mock.inject(function (_rootFct_, _$rootScope_) {
            $rootScope = _$rootScope_.$new();
            rootFct = _rootFct_;
        });
    });

    it('Set Amount in rootScope on 10', function() {
        var result = rootFct.getAmount();
        expect(result).toBe(13);
        $rootScope.amount = 15;
        result = rootFct.getAmount();
        expect(result).toBe(16);
    });
});

it only works when I am changing the

$rootScope = _$rootScope_.$new();

to

$rootScope = _$rootScope_;

and so I don't really understand when to use the $new() and for what its good for?

$new() is mainly used when creating a new scope out of an existing scope.

Your code is already injecting rootScope in the service so it would not hold any difference.

$new() can be used when you want to get some attributes from the parent scope with/without isolation.

It takes two params isolate and parent. Isolate (boolean) need to be used if isolation from parent scope is required. Parent (object) Explicitly defining the parent scope.

Do note that a new scope created manually needs to be destroyed manually as well.

More on it over here

这篇关于angularjs单元测试时使用$ rootScope。$新的()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆