当我尝试对我的控制器进行单元测试时,我收到了错误.当我调试测试用例时,我希望得到预期的值,但是它失败了以下错误.我在这里丢失的是什么,或者我是否在错误中测试控制器变量方式?
我的spec文件如下
'use strict';
describe('Information.controller', function () {
beforeEach(angular.mock.module('asp'));
var InformationController, scope;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
InformationController = $controller('InformationController', {
$scope: scope
});
}));
fit('Should remove the object without info from the object', function () {
InformationController.Data = [
{
"ban": "03745645455",
"accountNo": "0000drgyt456456565",
"id": "2c4cc5e8-f360367"
},
{
"ban": "27346fgh9080",
"accountNo": "000456456ytr655680",
"id": "2c4cc8ae-50bbf360367"
}
];
InformationController.banList = InformationController.Data.map((value, index) => (value && value.ban ? {'id': index, 'text': value.ban} : null))
.filter(value => value);
expect(InformationController.banList.length).toBe(3);
});
});
Run Code Online (Sandbox Code Playgroud) 我有一个组件A,它在模板中使用组件B,c,D:
###template-compA.html
<comp-b></comp-b>
<comp-c [myinput]="obj.myinput"></comp-c>
<comp-d ></comp-d>
Run Code Online (Sandbox Code Playgroud)
...等等
为简化起见,假设它们只是组件A中的一个指令:
###template-compA.html
<comp-b></comp-b>
Run Code Online (Sandbox Code Playgroud)
我的comp-b有自己的依赖项(服务或其他comp).
如果我想以这种方式测试comp-a:
TestBed.configureTestingModule({
declarations: [comp-A],
imports: [ReactiveFormsModule],
}).overrideComponent(FAQListComponent, {
set: {
providers: [
{ provide: comp-AService, useValue: comp-AListSVC }
]
}
})
.compileComponents();
Run Code Online (Sandbox Code Playgroud)
它不会正常工作.所以我这样做:
TestBed.configureTestingModule({
declarations: [comp-A, comp-B],
imports: [ReactiveFormsModule],
}).overrideComponent(FAQListComponent, {
set: {
providers: [
{ provide: comp-AService, useValue: comp-AListSVC }
]
}
})
.compileComponents();
Run Code Online (Sandbox Code Playgroud)
它也不起作用,因为comp-b没有自己的依赖项.在这里我很困惑,如果我必须每次导入和重新安装所有其他组件,我怎么能进行单元测试?它看起来像是一项非常大的工作.还有另外一种方法吗?使用具有自己的依赖关系的嵌套组件测试组件的最佳实践是什么?
非常感谢,
斯特凡.
我正在尝试为我的Angular控制器编写一个测试,我正在使用jasmine karma它angular-mocks,但一直在收到错误ReferenceError: Can't find variable: module.
我有一点搜索,但我已经有了angular-mocks我的凉亭.
我在这里可以缺少什么?
以下是我的代码:
#controller
angular.module('cook_book_ctrl', [])
.controller('cookBookCtrl', function($scope, CookBook, CookBookRecipesService){
$scope.cookbookoptions = true;
CookBook.list()
.success(function(data){
$scope.recipeList = data;
CookBookRecipesService.loadCookBookRecipes($scope.recipeList);
})
.error(function(error){
})
});
#controller test
describe('CookBook controller spec', function(){
var $httpBackend, $rootScope, createController, authRequestHandler
beforeEach(module('cook_book_ctrl'));
})
#bower.json
{
"name": "HelloIonic",
"private": "true",
"devDependencies": {
"ionic": "driftyco/ionic-bower#1.0.0",
"ionic-service-analytics": "master",
"ionic-service-core": "~0.1.4",
"angular-mocks": "1.3.13"
},
"dependencies": {
"ng-cordova-oauth": "~0.1.2",
"ng-tags-input": "~2.3.0",
"angular": "~1.4.0",
"underscore": "~1.8.3",
"materialize": …Run Code Online (Sandbox Code Playgroud) 在全新的 Angular 12 应用程序中运行单元测试时,我收到以下弃用警告:
(node:14940) [log4js-node-DEP0004] DeprecationWarning:模式 %d{DATE} 已被弃用,因为它在使用时会引起混乱。请改用 %d{DATETIME}。
why log4js提示"karma" depends on it。警告本身很清楚应该做什么,但缺少两个关键信息:
karma并用新语法替换已弃用的语法之外 - 我绝对不会这样做)。降级log4js到早期版本,不会输出警告,使用forceResolutions似乎不是一个好主意,特别是因为我发现了一些与其中的漏洞相关的github线程,尽管业力似乎没有受到影响。
问题:是否有可行的途径可以避免收到警告,或者“现在我们等待”(更新karma)是唯一的选择?
注意:我也在karma 的 repo上询问过。
unit-testing karma-runner karma-jasmine log4js-node angular-cli
我的角度应用程序工作得很好,我的测试也是如此,使用了业力和茉莉,直到我添加了一个依赖ui.bootstrap.现在应用程序仍然按预期工作,但我无法运行我的测试.这就是我所拥有的:
app.js - 在ui.bootstrap中添加了依赖项
angular.module('myApp', ['ngResource', 'ngRoute', 'ui.bootstrap']).config(function(...) {...});
Run Code Online (Sandbox Code Playgroud)
service.js
angular.module('myApp').service('myService', function () {})
Run Code Online (Sandbox Code Playgroud)
controller.js
angular.module('myApp').controller('MyController', function ($scope, $http, myService) {})
Run Code Online (Sandbox Code Playgroud)
测试/ main.js
describe('Controller: MyController', function () {
var MyController, scope;
// load the controller's module
beforeEach(function(){
module('myApp');
inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
MyController = $controller('MyController', {
$scope:scope
});
});
});
it('should do something', function () {
expect(scope.myOptions.length).toBe(5);
});
});
Run Code Online (Sandbox Code Playgroud)
我使用grunt和krama运行的测试因以下原因而失败:
Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed …Run Code Online (Sandbox Code Playgroud) javascript jasmine angularjs angular-ui-bootstrap karma-jasmine
我在测试Angular 2中的app.component.ts时遇到问题.我正在使用angular-cli.每当我运行测试时,我的app.component.spec.ts会使控制台提示错误:
Failed: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule'
Error: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule'
Run Code Online (Sandbox Code Playgroud)
我在TestBed中导入了HomeModuleComponent
TestBed.configureTestingModule({
declarations: [AppComponent],
imports : [ HomeModuleComponent ]
});
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我解决这个问题吗?
我一直在尝试使用karma-chrome-launcher运行我的测试,但每次运行我的测试时都会抛出此错误:
INFO [启动器]:启动浏览器Chrome错误[启动器]:无法启动Chrome
INFO [launcher]: Trying to start Chrome again (1/2).
ERROR [launcher]: Cannot start Chrome
INFO [launcher]: Trying to start Chrome again (2/2).
ERROR [launcher]: Cannot start Chrome
ERROR [launcher]: Chrome failed 2 times (cannot start). Giving up.
Run Code Online (Sandbox Code Playgroud)
这是我的karma.conf.js代码:
// Karma configuration
// Generated on Mon Mar 23 2015 14:04:19 GMT-0300 (BRT)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: 'www',
// frameworks to use
// available …Run Code Online (Sandbox Code Playgroud) 我有一些测试在PhantomJS中失败但在其他浏览器中失败.
我希望在我的监视任务中使用PhantomJS时忽略这些测试(因此新的浏览器窗口不需要关注并且perf更快一点),但在我的标准测试任务和我的CI管道中,我想要所有的测试在Chrome,Firefox等中运行...
我已经考虑了一个文件命名约定,比如foo.spec.dont-use-phantom.js我的Karma配置中的那些,但是这意味着我必须将失败的各个测试分离到他们自己的文件中,将它们与逻辑describe块分开并拥有更多文件奇怪的命名约定通常会很糟糕.
简而言之:
有没有办法可以扩展Jasmine和/或Karma并以某种方式注释单个测试只能运行某些配置?
我已经提到了以下链接来获得答案,但我找不到任何适用于我的方案的解决方案. 错误:(SystemJS)无法解析ActivatedRoute的所有参数:(?,?,?,?,?,?,?,?)
因此,我一直试图从供应商中删除激活路线,但测试床仍未通过.表明
错误:没有ActivatedRoute的提供商!
所以这是我的代码,我想在使用Jasmine的角度应用程序中运行我的测试床.
import { ActivatedRoute } from '@angular/router';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterModule, Routes } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
describe('SomeComponent', () => {
let component: SomeComponent;
let fixture: ComponentFixture<SomeComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ RouterModule, RouterTestingModule ],
declarations: [ SomeComponent ],
providers: [ ActivatedRoute ],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => …Run Code Online (Sandbox Code Playgroud) 升级到 Angular 13 后,使用--code-coverage运行的测试失败,并出现缺少某些插件的错误
\n我正在使用karma-coverage-istanbul-reporter,karma.conf.js这导致了一些问题。我什至将其替换为默认值karma-coverage,但仍然看到相同的错误
ng test waxion --no-watch --code-coverage --browsers ChromeHeadless\nRun Code Online (Sandbox Code Playgroud)\n\xe2\xa0\x8b Generating browser application bundles (phase: setup)...20 11 2021 17:34:24.723:ERROR [reporter]: Can not load reporter "coverage", it is not registered!\n Perhaps you are missing some plugin?\n\xe2\xa0\x99 Generating browser application bundles (phase: building)...20 11 2021 17:34:27.353:INFO [karma-server]: Karma v6.3.9 server started at http://localhost:9876/\n20 11 2021 17:34:27.353:INFO [launcher]: Launching browsers Chrome with concurrency unlimited\n20 11 2021 17:34:27.353:ERROR [karma-server]: Error: Found …Run Code Online (Sandbox Code Playgroud) karma-jasmine ×10
unit-testing ×5
jasmine ×4
angular ×3
angularjs ×3
angular-cli ×2
javascript ×2
karma-runner ×2
log4js-node ×1
router ×1
testbed ×1
testing ×1