【问题标题】:Trivial angular test for a beginner初学者的简单角度测试
【发布时间】:2014-06-21 00:37:30
【问题描述】:

我正在尝试使用 Angular js 进行单元测试,但这并不容易。

我想像这样进行一个简单的琐碎测试:

describe('Edition Controllers', function(){
  beforeEach(module('DOPCjsControllers.controller'));


it('should check a simple test', inject(function($controller) {
  var scope = {};
  var editionCtrl = $controller('EditionController', {$scope : scope} );
  expect(scope.test).toBe("lol");

   }));
});

我的控制器开始了:

'use strict';
/* Controllers */

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

/**
*EditionController : Controlleur de gestion de la partie edition du logiciel.
*/
DOPCjsControllers.controller('EditionController', ['$scope', '$http', '$routeParams', '$filter', '$modal','$fileUploader', 'AUTH_EVENTS','NotificationFactory', 'NOTIF_STATUTS','Session','AuthService','$location',
function($scope, $http, $routeParams, $filter, $modal, $fileUploader, AUTH_EVENTS, NotificationFactory, NOTIF_STATUTS, Session, AuthService, $location){
    $scope.test = "lol";
    $scope.$on(AUTH_EVENTS.notAuthenticated, function(event){
        //alert("Connectez vous d'abord");
        console.log("notAuthenticated");

    }); etc ...

当我执行测试时,我看到: Firefox 29.0.0 (Ubuntu):执行 1 of 1 (1 FAILED) 错误 (0.645 秒 / 0.011 秒) 信息 [观察者]:更改文件“/home/lionnel/angular-seed/test/unit/controllersSpec.js”。 Firefox 29.0.0 (Ubuntu) 版控制器应该检查一个简单的测试失败 minErr/

你知道有什么诀窍吗?

感谢您的帮助

不幸的是,当我写的时候:

describe('Edition Controllers', function(){
beforeEach(module('DOPCjsControllers'));


    it('should check a simple test', inject(function($controller) {
      var scope = {};
      var editionCtrl = $controller('EditionController', {$scope : scope} );
      expect(scope.test).toBe("lol");

    }));
});

我有这个错误信息: 错误:[$injector:unpr] 未知提供者:$routeParamsProvider http://errors.angularjs.org/1.2.16/$injector/unpr?p0=%24routeParamsProvider%20%3C-%20%24routeParams

【问题讨论】:

  • 您可能需要在测试中手动触发摘要循环。尝试把 scope.$digest();期待之前的电话。哦...您可能还想注入 $rootScope,并使用 $rootScope.$new(); 创建测试范围;

标签: angularjs unit-testing controller jasmine karma-runner


【解决方案1】:

您的测试尝试加载名为“DOPCjsControllers.controller”的模块。你没有任何这样的模块。您定义的模块称为“DOPCjsControllers”:

var DOPCjsControllers = angular.module('DOPCjsControllers', []);
        That's the name of the module ----^

【讨论】:

    猜你喜欢
    • 2010-10-03
    • 2015-04-27
    • 2021-09-02
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多