简体   繁体   中英

Jasmine Unit Testing Angularjs

I'm new to Unit Testing. I have followed the tutorials and I have everything configured on node.js via npm. I have done some describe and it just to get the feel for ho things are set up and my spec runner is fine. The problem I'm trying to get test on controllers figured out but I run in a snag and been trying to figure things out for a while but I continue to get the same error so I thought I would reach out.

I'm trying to do a simple test on a LoginController but I continue to get the same error. I can't point out what I'm doing wrong. Trying to get over this hurdle.

TypeError: angular.mock.module is not a function:

spec runner index html file.

<!doctype html>
   <html>
 <head>
<title>Jasmine Spec Runner</title>
<link rel="stylesheet" href="../bower_components/jasmine-core/lib/jasmine-core/jasmine.css">
</head>
<body>
<script src="../My Documents/My Website/AngularLogin-
      Registration/js/angular-1.6.0.js"></script>
<script src="../My Documents/My Website/AngularLogin-Registration/js/angular-route-1.6.0.min.js"></script>

<script src="../bower_components/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script src="../bower_components/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
<script src="../bower_components/jasmine-core/lib/jasmine-core/boot.js"></script>

<!-- include source files here... -->


<!--<script src="//code.jquery.com/jquery-3.1.1.min.js"></script>-->
<!--<script src="//code.angularjs.org/1.6.0/angular-cookies.min.js"></script>-->
<script src="../My Documents/My Website/AngularLogin-Registration/js/angular-mock.js"></script>
<script src="../My Documents/My Website/AngularLogin-Registration/js/app.js"></script>
<script src="../My Documents/My Website/AngularLogin-Registration/login/login.controller.js"></script>

<!-- include spec files here... -->
<script src="spec/test.js"></script>

Here is my test file.

describe('LoginController test', function () {
    beforeEach(angular.mock.module('app'));
    beforeEach(angular.mock.inject(function(_$controller_){
        $controller = _$controller_;
}));
describe('$scope.grade', function() {
      it('sets the strength to "strong" if the password length is >8 chars', 
       function() {
        var $scope = {};
        var controller = $controller('LoginController', { $scope: $scope });
  $scope.password = 'longerthaneightchars';
  $scope.grade();
      expect($scope.strength).toEqual('strong');
    });
       });
          });

Thanking You In Advance PDH

Load angular-mocks library in your spec runner html , make sure to load it after angular.js.

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-mocks.js"></script>

You can also use bower to download the js file instead of the CDN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM