0

I am begining with Angular and with unit testing, this is my first code, and it isn't working. I searched for a solution but I dont know what I am doing wrong. If anyone can explain to me what is the mistake, I would thank you.

This is all my code.

<!DOCTYPE html>
<html lang="en">

<head>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-mocks.js"></script>
<script src="jasmine.js"></script>
<script src="jasmine-html.js"></script>
<script src="boot.js"></script>
<link rel="shortcut icon" type="image/png" href="jasmine_favicon.png">
<link rel="stylesheet" href="jasmine.css">

</head>

<body  ng-app = "myApp">

<div ng-controller = "MyCtrl">
{{greeting}}
</div>
<br><br><br>

<script>

    <!-- CODE -->

    var myApp = angular.module('myApp',[]);

    myApp.controller('MyCtrl', ['$scope', function($scope) {
        $scope.greeting = 'Hello World!';
    }]);

    <!-- JASMINE -->

    describe('myApp', function () {
        var scope,
        controller;
        beforeEach(function () {
            module('myApp');
        });

        describe('MyCtrl', function () {
            beforeEach(inject(function ($rootScope, $controller) {
                scope = $rootScope.$new();
                controller = $controller('MyCtrl', {
                    '$scope': scope
                });
            }));
            it('sets the greeting', function () {
                expect(scope.greeting).toBe('Hello World!');
            });

        });
    });

    describe('JavaScript addition operator', function () {
        it('adds two numbers together', function () {
            expect(1 + 2).toEqual(3);
        });
    });
</script>

</body>

</html>

Thanks in advance

4
  • I think you meant to type angular.module('myApp') in your beforeEach function. Commented Jun 6, 2016 at 21:08
  • That remove the "ReferenceError: module is not defined" but the "ReferenceError: inject is not defined" remains. what can I do for that particular mistake? Thanks Commented Jun 6, 2016 at 21:15
  • Oh, I see. You need to load Jasmine before loading Angular. Move your <script> tags for jasmine.js and jasmine-html.js above the one for angular. That's how angular-mocks.js detects the testing environment. After that, you can just use module instead of angular.module again. Commented Jun 6, 2016 at 21:27
  • it didn't change the status. "ReferenceError: inject is not defined" remains and by moving the <script> tags of jasmine and jasmine-html above the ones of angular and angular-mock it doesn't allow me to remove the angular.module. @MikeC Commented Jun 6, 2016 at 21:43

1 Answer 1

2

The sequence of your scripts is wrong. You should first include the scripts related to Jasmine, and then the scripts for angular and angular-mocks.

<!DOCTYPE html>
<html lang="en">

<head>
<!--Scripts for Jasmine-->
<script src="jasmine.js"></script>
<script src="jasmine-html.js"></script>
<script src="boot.js"></script>

<!--Script for Angular-->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

<!--Scripts for Angular-Mocks-->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-mocks.js"></script>
<link rel="shortcut icon" type="image/png" href="jasmine_favicon.png">
<link rel="stylesheet" href="jasmine.css">
</head>

<body  ng-app = "myApp">

<div ng-controller = "MyCtrl">
{{greeting}}
</div>
<br><br><br>

<script>

<!-- CODE -->

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', ['$scope', function($scope) {
    $scope.greeting = 'Hello World!';
}]);

<!-- JASMINE -->

describe('myApp', function () {
    var scope,
    controller;
    beforeEach(function () {
        module('myApp');
    });

    describe('MyCtrl', function () {
        beforeEach(inject(function ($rootScope, $controller) {
            scope = $rootScope.$new();
            controller = $controller('MyCtrl', {
                '$scope': scope
            });
        }));
        it('sets the greeting', function () {
            expect(scope.greeting).toBe('Hello World!');
        });

    });
});

describe('JavaScript addition operator', function () {
    it('adds two numbers together', function () {
        expect(1 + 2).toEqual(3);
    });
});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.