Reputation: 8663
I am trying to write unit tests for my code for the first time. I can test $scopes when declared at the top of my controllers, but how can i test $scopes that are inside a $scope function?
Ctrl:
app.controller("MyCtrl", function($scope, $http, myService) {
$scope.initialiseValue = 0;
$scope.address = function (value) {
$scope.addressValue = 0;
}
});
Test:
describe('app', function () {
var app;
beforeEach(function () {
app = angular.mock.module('app')
});
describe('MyCtrl', function () {
var scope, ctrl, theService , httpMock;
beforeEach(inject(function($controller, $rootScope, myService, $httpBackend) {
scope = $rootScope.$new(),
ctrl = $controller('MyCtrl', {
$scope: scope,
$location : location,
myService: theService ,
$httpBackend: httpMock
});
}));
// This test succeeds ///////////
it('initialiseValue should be initialised to 0', function() {
console.log(scope.initialiseValue );
expect(scope.initialiseValue ).toBe(0);
});
// This test fails ///////////
it('addressValue should be initialised to 0', function() {
console.log(scope.addressValue );
expect(scope.addressValue ).toBe(0);
});
});
Error:
Expected undefined to be false.
Using Karma and Jasmine.
Any help would be appreciated.
Upvotes: 0
Views: 39
Reputation: 1738
You forgot to call the
scope.address()
function inside the test. If you do not call this function,
$scope.addressValue
can not be updated. Updated working fiddle http://jsfiddle.net/themyth92/j3pkaebw/1/
Upvotes: 1