ReferenceError:模块未在Jasmine Angular js中定义

时间:2016-06-06 21:06:19

标签: javascript angularjs jasmine

我开始使用Angular和单元测试,这是我的第一个代码,它不起作用。我搜索了一个解决方案,但我不知道我做错了什么。如果有人能向我解释这是什么错误,我会感谢你。

这是我的所有代码。

<!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>

提前致谢

1 个答案:

答案 0 :(得分:2)

您的脚本序列错误。您应首先包含与Jasmine相关的脚本,然后包括角度和角度模拟的脚本。

<!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);
    });
});