TypeError:无法使用Jasmine读取未定义Angular的属性'$ injector'

时间:2014-05-26 11:25:55

标签: javascript angularjs jasmine

我创建了一个Angular应用程序,我想用Jasmine测试它。 我创建了一个名为testExample.html的页面。我想谈谈并运行测试。

不幸的是,我无法运行它。我收到一个错误 - TypeError:无法读取未定义的属性'$ injector'。

testExample.html


<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Jasmine Spec Runner v2.0.0</title>

  <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.0.0/jasmine_favicon.png">
  <link rel="stylesheet" type="text/css" href="lib/jasmine-2.0.0/jasmine.css">

  <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine.js"></script>
  <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine-html.js"></script>
  <script type="text/javascript" src="lib/jasmine-2.0.0/boot.js"></script>


   <!-- JavaScript libraries on which source depends -->

  <script src="../Example/js/angular.js"></script>

  <script src="../Example/js/angular-mocks.js"></script>

  <!-- include source files here... -->
  <script type="text/javascript" src="../Example/js/example.js"></script>
  <script type="text/javascript" src="../Example/js/entities.js"></script>
  <script type="text/javascript" src="../Example/js/helperCtrl.js"></script>  


  <!-- include spec files here... -->
  <script type="text/javascript" src="spec/ExampleSpec.js"></script>

</head>
<body>
</body>
</html>

Example.js:

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

app.controller('mainCtrl', function($scope, $http,$timeout, helperCtrl) {

    $scope.customers = helperCtrl.initCustomers();

    $scope.jobs = helperCtrl.initJobs();

    $scope.customerPointer ="";
    $scope.jobPointer ="";

    $scope.selectCustomer = function(index){
        $scope.customerPointer = $scope.customers[index];
    };

    $scope.selectJob = function(index){
        $scope.jobPointer = $scope.jobs[index];
    };

    $scope.addJobToUser = function(){
        if ($scope.customerPointer != "" && $scope.jobPointer != "")
            $scope.customerPointer.addJob($scope.jobPointer);
    };
});

测试代码:exampleSpec.js

 describe('testSuite', function () {
//mock Application to allow us to inject our own dependencies
beforeEach(angular.mock.module('main'));
//mock the controller for the same reason and include $rootScope and $controller
beforeEach(angular.mock.inject(function($rootScope, $controller){

    //create an empty scope
    scope = $rootScope.$new();
    //declare the controller and inject our empty scope
    $controller('MainCtrl', {$scope: scope});
}));

describe('mainCtrl', function () {
    beforeEach(inject(function ($rootScope, $controller) {
        scope = $rootScope.$new();
        controller = $controller('mainCtrl', {
            '$scope': scope
        });
    }));
    it('select a user', function () {
        var index = 0;
        scope.selectCustomer(index);
        expect(scope.customerPointer.getName()).toBe('Aviad');
    });

    it('select a job', function () {
        var index = 0;
        scope.selectJob(index);
        expect(scope.jobPointer.getName()).toBe('Software Developer');
    });

    it('add job to user', function () {
        var index = 0;
        scope.selectJob(index);
        scope.selectCustomer(index);
        expect(scope.customerPointer.jobs).toBe(scope.jobPointer);
    });
});   
});

我做错了什么?

0 个答案:

没有答案