testing - 如何在 karma 测试中通过 AngularJS $scope

标签 testing angularjs scope inject karma-runner

我开始用 Karma 编写一些测试,为了求助,我决定尝试简单的测试。但是,我收到两个错误(一个与 Jasmine 有关,另一个与注入(inject)有关(我在尝试 angular.inject 时遇到同样的错误):

two errors Firefox 22.0 (Windows) ConfigurationController encountered a declaration exception FAILED
    TypeError: this.func.apply is not a function in 
/adapter/lib/jasmine.js?1374202126000 (line 1145)
    testacular.js:106
    :9876/context.html:40

    ReferenceError: inject is not defined in     /var/lib/tomcat7/webapps/lunchtime/test/controllers/configuration-controller.js (line 7)
    @/var/lib/tomcat7/webapps/lunchtime/test/controllers/configuration-controller.js:7
    @/var/lib/tomcat7/webapps/lunchtime/test/controllers/configuration-controller.js:3

Firefox 22.0 (Windows):执行 1 次,共 1 次(1 次失败)(0.48 秒/0.011 秒)

我有一个简单的 Controller :

app.controller("ConfigurationController", ["$scope", "$http", function($scope, $http) {
$scope.configuration = {};
}]);

简单测试:

'use strict';

describe('ConfigurationController', function() {
var scope, ctrl;
//you need to indicate your module in a test
beforeEach(angular.module('AmphinicyLunch'));
beforeEach(inject(function($rootScope) {
    scope = $rootScope.$new();
    ctrl = $controller("ConfigurationController", {$scope: scope})
}));

it("should have defined configuration", function($scope) {
    dump($scope.configuration);
    expect($scope.configuration).toEqual({});
});

});

最佳答案

对于注入(inject)错误,你需要包含angular-mocks.js。模块和注入(inject)都在该文件中定义。恐怕我不知道 Jasmine 错误。

关于testing - 如何在 karma 测试中通过 AngularJS $scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17912181/

相关文章:

css - 使用 webpack 更改路由时如何卸载 css?

javascript - Angular 从链接函数内带有参数的指令调用 Controller 方法

javascript - 刷新 html 以了解指令一致模型更改

javascript - jquery javascript 关于加载变量范围

javascript - 分机组件 : Template's afterrender fails to access renderSelectors

macos - 向 Beta 测试人员分发沙盒 Mac 应用程序的最佳方式

testing - 如何在 playframework 中测试 Jobs?

testing - JMeter- 从 CLI 运行时不会替换属性值

ruby-on-rails - 在助手规范中测试当前用户

php变量作用域