lundi 16 février 2015

angularjs unit test when to use $rootScope.$new()

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?


Aucun commentaire:

Enregistrer un commentaire