单元测试如何启动控制器(Ctrl未注册)

时间:2018-06-15 14:02:48

标签: angularjs unit-testing testing jasmine automated-tests

我实际上正在使用karma + jasmine进行单元测试,我有一些问题。

我有一个控制器,我想用茉莉花来测试。但我不明白怎么称呼它。

我的控制器要求一些服务和当地工厂。

var app = angular.module('App', ['ngMaterial', 'ngMessages'])
app.controller('Ctrl', ['$rootScope', '$scope','$http','$timeout','$mdDialog','Global', 'projects', 'screens', 'users', 'licenses', function($rootScope, $scope, $http, $timeout, $mdDialog,Global, projects, screens, users, licenses) { 
...
}

这是我的测试文件:

describe('MainController',()=>{

  beforeEach(()=>{angular.module('App')});

  var controller,scope,rootScope,createController,projects,users,licenses,screens,http,mdDialog;

  beforeEach(inject((_$rootScope_,$controller,_$httpBackend_,_$timeout_)=>{
    scope = _$rootScope_.$new();
    rootScope = _$rootScope_;
    controller = $controller('Ctrl',{$scope : scope});
  }));

  it('should be defined',()=>{
    expect(controller).toBeDefined();
  })
});

我的index.html:

<html lang="en">
   <head>
      <title>ByOnSite</title>
      <meta charset="utf-8">
      <link rel="icon" href="logo.ico" type="image/x-icon" />
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <meta name="robots" content="noindex, nofollow">
      <meta name="author" content="Bouygues TP TPINST">
      <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.css">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" crossorigin="anonymous">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.min.css">
      <link rel="stylesheet" href="style.css">
      <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" crossorigin="anonymous"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"crossorigin="anonymous"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/js/bootstrap-select.min.js"></script>
      <script src="main.js"></script>
      <script src="global.js"></script>
      <script src="projects.js"></script>
      <script src="users.js"></script>
      <script src="screens.js"></script>
      <script src="licenses.js"></script>
      <script src="authInterceptor.js"></script>
   </head>

   <body style="height:auto;">
      <div ng-app="App">
         <div ng-controller="Ctrl">
...

我的业力配置文件:

// Karma configuration
// Generated on Thu Jun 14 2018 16:51:15 GMT+0200 (Paris, Madrid (heure d’été))

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [ 'Scripts/angular.js','Scripts/angular-mocks.js','main.js','*.js'
    ],


    // list of files / patterns to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

这是我的控制台中的错误:

Error: [$controller:ctrlreg] The controller with the name 'Ctrl' is not registered.

你能帮我吗?

0 个答案:

没有答案