- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的karma/karma.conf.js
:
// Karma configuration
// Generated on Mon Jan 04 2016 16:17:18 GMT-0500 (EST)
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: [
'../angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'../post.js',
'tests/test_post.js'
],
// list of files 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: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
这是我的karma/tests/test_post.js
:
describe('Controller: MainCtrl', function() {
beforeEach(module('PostPageApp'));
var ctrl;
beforeEach(inject(function($controller) {
ctrl = $controller('MainCtrl');
}));
it('Show have an add and logout function', function() {
expect(ctrl.add).toBeDefined();
});
});
这是../post.js
:
angular.module("PostPageApp", ["BaseApp"])
.controller("MainCtrl", ["$http", "$window", "BaseService", function($http, $window, BaseService) {
var self = this;
self.add = function() {
BaseService.add.post(self.post, function() {
self.cerrorMessages = BaseService.cerrorMessages;
});
};
self.logoutUser = function() {
BaseService.logout();
};
}]);
现在,当我执行 karma start
时,它会返回:
TypeError: Cannot read property 'add' of undefined
at Object.<anonymous> (/home/user/Documents/CMS/CMSApp/static/js/karma/tests/test_post.js:11:20)
Chromium 47.0.2526 (Ubuntu 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.075 seChromium 47.0.2526 (Ubuntu 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.118 secs / 0.075 secs)
即使我有 ctrl = $controller('MainCtrl');
这行,它也无法读取未定义的属性 add
。知道为什么它说 Controller 未定义吗?
最佳答案
您编写 Controller 的方式与编写服务的方式非常相似(使用“this”而不是“$scope”)。我从未真正见过以这种方式编写的 Controller ,这可能会使对其进行单元测试变得更加困难。
我在您的测试中看到的主要问题是 ctrl
变量未分配给语句中的任何范围,
beforeEach(inject(function($controller) {
ctrl = $controller('MainCtrl');
}));
如果您使用 $scope
而不是 this
,测试通常如下所示:
describe('Controller: MainCtrl', function() {
beforeEach(module('PostPageApp'));
var ctrl, scope;
beforeEach(inject(function($controller, $scope) {
scope = $rootScope.$new();
ctrl = $controller('MainCtrl', {
scope: $scope
});
}));
it('should have an add function', function() {
expect(scope.add).toBeDefined();
});
});
并且 Controller 本身将使用 $scope
而不是 this
。
angular.module("PostPageApp", ["BaseApp"])
.controller("MainCtrl", ["$http", "$window", "$scope", "BaseService", function($http, $window, $scope, BaseService) {
var scope = $scope;
scope.add = function() {
BaseService.add.post(scope.post, function() {
scope.cerrorMessages = BaseService.cerrorMessages;
});
};
scope.logoutUser = function() {
BaseService.logout();
};
}]);
这是一种实现方法,但您似乎不想使用 $scope
。我猜这是因为您只是不想在全局范围内公开您的 Controller ,这很好。你的方式甚至可能比传统方式更好。我用 Google 搜索了一下,发现 Unit testing with 'this' .它看起来很有帮助,读起来很有趣;尽管我希望他已经包含了他正在测试的源代码。看起来他只是使用了 vanilla JavaScript,在我看来这比 Angular 更容易测试。
我知道我没有为您编写 Controller 的方式提供完整的测试解决方案,但我希望我能帮助您查明问题并让您更好地理解一般的单元测试 Angular Controller 。
关于javascript - Karma/Jasmine 没有找到 AngularJS Controller (说它是未定义的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34601460/
我使用 File > Settings > JavaScript > Libraries 在 Webstorm 8.0.4 中设置 Jasmine 集成。我添加了 karma-jasmine带有我的
假设我有 spyOn($cookieStore,'get').and.returnValue('abc'); 对于我的用例来说,这太笼统了。任何时候我们打电话 $cookieStore.get('so
我正在阅读有关 Jasmine 测试的教程,它建议您应该使用标准 Jasmine 目录树结构(这似乎与 RSpec 目录树结构有关)。我用谷歌搜索了一个小时,找不到任何有关此标准目录树的信息。 考虑到
问题 如何使用Jasmine插件通过Jasmine运行Karma时更新所使用的karma-jasmine的版本? 仅当Jasmine插件集成了较新版本的karma-jasmine时,才会更新Jasmi
我想在每个规范执行后获得测试的通过或失败状态: var passed = jasmine.getEnv().currentSpec.results().passed(); if (!pas
我有一个与 Karma 一起运行的 Jasmine 测试: describe('When a logged in user chooses Rent and Payment PIN is enable
在我的新手理解中,Jasmine 提供了以下语法: describe('FooBar', function(){ it('should blah-blah', function(){ ex
我认为我通过编写这样的 Jasmine 模拟取得了很好的进展。但我无法修复这个错误。 spyOn 方法实际上是如何工作的?事实上这种方法似乎被广泛使用。 2.0 有什么变化 describe('Tes
有大量文档展示了如何将匹配器添加到 Jasmine 规范(例如 here )。 有人找到了向整个环境添加匹配器的方法吗?我想创建一组有用的匹配器,供任何和所有测试调用,而无需在我的规范中复制粘贴。 目
我正在尝试查找所有现有 Jasmine 期望匹配器(例如“toContain”等)的列表... 哪里可以找到这个?我搜索了一段时间,但找不到像 api 这样的东西。 Jasmine 网站也没有任何类型
我熟悉 python 单元测试测试,如果断言失败,该测试将被标记为“失败”,并继续进行其他测试。另一方面,即使其中一个失败, Jasmine 也会继续完成所有期望。如何让 Jasmine 在第一个期望
工具:Protractor 3.3.0、Jasmine 2.4.1、Selenium Standalone Server。 我有一个测试套件,里面有大量的 spec.js 文件,每个文件都包含我的应用
Jasmine 中是否有与 console.log 类似的方法?如果我想注销我 mock 的 $controller 或服务的值,有没有办法做到这一点? 最佳答案 您应该只使用 console.log
我已经通过运行 npm install jasmine -g 全局安装了 jasmine。 运行 jasmine -v 给我 jasmine v2.5.0 jasmine-core v2.5.0 然后
我有gulp-jasmine-phantom正在运行,但我得到 ReferenceError: Tictactoe is not defined 。我感觉我犯了一些根本性的错误。 我的文件结构: gu
我已经创建了一个 JavaScript 项目,需要使用 Jasmine 进行单元测试。我遵循了以下步骤: 1. npm install jasmine --save-dev - 从项目根目录 2.现在
我正在为我的应用程序使用 Yeoman+Angular Generator,我一直在努力与 Jasmine 相处!这就是我卡住的地方。我希望能够在 Jasmine 测试中使用 jQuery 选择器。我
it('should for something', function check(done) { browser.sleep(2000); $('.csTx').isPresent().th
Javascript 编码有点新,所以请耐心等待。 我阅读了以下链接 jasmine with jscoverage automated testing 但是,由于我没有 Ruby 项目,因此它似乎不
在Jasmine headless (headless)Webkit中运行测试时,我遇到了一个简单的TypeError: 'undefined' is not an object失败。但是没有提示在哪
我是一名优秀的程序员,十分优秀!