squ*_*hel 2 javascript unit-testing jasmine angularjs
我真的不明白我应该在角度单位测试中使用$ rootScope = $ rootScope.$ new().你在许多单元测试例子中看到了这个断言.但在以下示例中不起作用:
angular.module("app.root", []).factory("rootFct", function ($rootScope) {
$rootScope.amount = 12;
return {
getAmount: function () {
return ($rootScope.amount + 1);
}
}
});
Run Code Online (Sandbox Code Playgroud)
关联的单元测试不起作用:
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);
});
});
Run Code Online (Sandbox Code Playgroud)
它只适用于我改变它的时候
$rootScope = _$rootScope_.$new();
Run Code Online (Sandbox Code Playgroud)
至
$rootScope = _$rootScope_;
Run Code Online (Sandbox Code Playgroud)
所以我真的不明白何时使用$ new()以及它的好处?