【发布时间】:2015-10-02 17:35:48
【问题描述】:
我正在尝试使用 Jasmine 和 Karma 测试我的 AngularJS 控制器。 当我尝试使用documented procedure 配置控制器测试时,$scope 没有被创建。实际上看起来之前根本没有执行。
这是我的控制器声明:
angular.module("myApp", ["ngCookies"])
.controller("myCtrl", function($scope, $window, $location, MyService,
$timeout, $log, $cookies, $q) {
我的测试如下所示:
describe("MyController test", function() {
var $scope;
beforeEach(
module("myApp", ["ngCookies"]));
beforeEach(inject(function($rootScope, $controller) {
$scope = $rootScope.$new(); // It looks like this is never called
scope =$scope;
$controller("myCtrl", {$scope: $scope});
}));
it("Checks the state of various units", function() {
...
});
看起来 beforeEach 的东西从来没有被调用过。我想我需要将我的其余属性注入控制器,但如果是这样,我不确定如何。
这是测试运行日志中的错误
FAILED MyController test Checks myFunction starting from blank state
Error: [$injector:modulerr] Failed to instantiate module ngCookies due to:
Error: [ng:areq] Argument 'fn' is not a function, got string
http://errors.angularjs.org/1.3.14/ng/areqp0=fn&p1=not%20a%20function%2C%20got%20string
【问题讨论】:
-
我看到的东西非常相似。就我而言,部分原因是inject() 抛出异常(在调用回调之前)“模块'angulartics'不可用!您拼错了模块名称或忘记加载它。如果注册模块确保您将依赖项指定为第二个参数。”我以与您传递 ngCookies 相同的方式传递 angularitics。而且我确定我已经包含它,只是可能在错误的地方?希望这里提出的任何解决方案也可以帮助我!
标签: angularjs jasmine karma-jasmine