简体   繁体   English

茉莉花单元测试Angularjs

[英]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. 我已经按照教程学习,并通过npm在node.js上进行了所有配置。 I have done some describe and it just to get the feel for ho things are set up and my spec runner is fine. 我已经做了一些描述,这只是为了让人们对ho事物有所感觉,而我的spec赛跑者也很好。 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. 我正在尝试对LoginController做一个简单的测试,但我仍然遇到相同的错误。 I can't point out what I'm doing wrong. 我不能指出我做错了什么。 Trying to get over this hurdle. 试图克服这个障碍。

TypeError: angular.mock.module is not a function: TypeError:angular.mock.module不是函数:

spec runner index html file. 规格跑步者索引html文件。

<!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 预先感谢您PDH

Load angular-mocks library in your spec runner html , make sure to load it after angular.js. 在您的规范运行器html中加载angular-mocks库,请确保在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. 您还可以使用Bower下载js文件而不是CDN。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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