I am new with AngularJS, so for learning purpose..I have built a very simple app which has one title as "Hello friends!" and a button named "Change the title!", on click it results to the change in the title as "How are you?"...
Now for doing Unit-testing, I wrote testcases.. but faces unusual errors like:
Chrome 35.0.1916 (Windows 7) titleCtrl Intially has a title FAILED, Error: [$injector:modulerr] http://errors.angularjs.org/1.2.20/$injector
TypeError: Cannot read property 'title' of null at null: C:/Myapp1/test/test.js:21
Chrome 35.0.1916 (Windows 7) titleCtrl Clicking the button changes the title FAILED, Error: [$injector:modulerr] http://errors.angularjs.org/1.2.20/$injector
TypeError: Cannot read property 'changeIt' of null: C:/Myapp1/test/test.js:26
So, for your reference am adding these files: scope.html, controller.js and test.js :
scope.html
<!doctype html>
<html ng-app="app">
<head runat="server">
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="C:/Myapp1/bower_components/angular-mocks/angular-mocks.js"></script>
<script type="text/javascript" src="controller.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs-1.2.8/angular-route.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular-resource.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div ng-controller= "titleCtrl">
<h1 ng-model = "title">{{title}}</h1>
<button ng-click="changeIt()">Change the title!</button>
</div>
</form>
</body>
</html>
controller.js
var myModule = angular.module('app',[]);
var $scope = null;
myModule.controller('titleCtrl', function($scope)
{
$scope.title="Hello friends!";
$scope.changeIt = function()
{
$scope.title = "How are you?";
};
});
test.js
describe('titleCtrl', function()
{
var $controller = null;
var $scope = null;
beforeEach(function()
{
module('myModule');
});
beforeEach(inject(function($controller,$rootScope)
{
$scope = $rootScope.$new();
controller = $controller('titleCtrl',
{
$scope: $scope
});
}));
it('Intially has a title', function()
{
expect($scope.title).toBe("Hello friends!");
});
it('Clicking the button changes the title', function()
{
$scope.changeIt();
expect($scope.title).toBe("How are you?");
});
});
Am kind a stuck with this error.. Any help will be appreciated..