在我的组件中使用templateUrl时,我在运行带有Karma的Angular 2测试时遇到一些问题。
这是我的karma配置文件:
'use strict';
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: './',
autoWatchBatchDelay: 3000,
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'node_modules/zone.js/dist/zone-microtask.js',
'node_modules/zone.js/dist/long-stack-trace-zone.js',
'node_modules/zone.js/dist/jasmine-patch.js',
'node_modules/es6-module-loader/dist/es6-module-loader.js',
'node_modules/traceur/bin/traceur-runtime.js', // Required by PhantomJS2, otherwise it shouts ReferenceError: Can't find variable: require
'node_modules/traceur/bin/traceur.js',
'node_modules/systemjs/dist/system.src.js',
'node_modules/reflect-metadata/Reflect.js',
// beta.7 IE 11 polyfills from https://github.com/angular/angular/issues/7144
'node_modules/angular2/es6/dev/src/testing/shims_for_IE.js',
{ pattern: 'node_modules/angular2/**/*.js', included: false, watched: false },
{ pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false },
{ pattern: 'dist/dev/**/*.js', included: false, watched: true },
{ pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: false, watched: false }, // PhantomJS2 (and possibly others) might require it
'test-main.js'
],
// list of files to exclude
exclude: [
'node_modules/angular2/**/*spec.js'
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'dist/**/!(*spec).js': ['coverage']
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['mocha', 'coverage'],
coverageReporter: {
dir: 'coverage/',
reporters: [
{ type: 'text-summary' },
{ type: 'json', subdir: '.', file: 'coverage-final.json' },
{ type: 'html' }
]
},
// 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: [
'PhantomJS'
],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
下面是一个示例模块:
import {Component} from 'angular2/core';
import {CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/common';
import {NameList} from '../../shared/services/name_list';
@Component({
selector: 'about',
moduleId: module.id, // using relative paths, see http://schwarty.com/2015/12/22/angular2-relative-paths-for-templateurl-and-styleurls/
templateUrl: './about.html',
styleUrls: ['./about.css'],
directives: [FORM_DIRECTIVES, CORE_DIRECTIVES]
})
export class AboutCmp {
newName;
constructor(list = new NameList()) {
this.list = list;
}
/*
* @param newname any text as input.
* @returns return false to prevent default form submit behavior to refresh the page.
*/
addName() {
this.list.add(this.newName);
this.newName = '';
return false;
}
}
和我的测试规范文件:
import {
TestComponentBuilder,
describe,
expect,
injectAsync,
it
} from 'angular2/testing';
import {Component} from 'angular2/core';
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
import {AboutCmp} from './about';
import {NameList} from '../../shared/services/name_list';
export function main() {
describe('About component', () => {
it('should work',
injectAsync([TestComponentBuilder], (tcb) => {
return tcb.createAsync(TestComponent)
.then(rootTC => {
rootTC.detectChanges();
let aboutInstance = rootTC.debugElement.children[0].componentInstance;
let aboutDOMEl = rootTC.debugElement.children[0].nativeElement;
let nameListLen = function () {
return aboutInstance.list.names.length;
};
expect(aboutInstance.list).toEqual(jasmine.any(NameList));
expect(nameListLen()).toEqual(4);
expect(DOM.querySelectorAll(aboutDOMEl, 'li').length).toEqual(nameListLen());
aboutInstance.newName = 'Minko';
aboutInstance.addName();
rootTC.detectChanges();
expect(nameListLen()).toEqual(5);
expect(DOM.querySelectorAll(aboutDOMEl, 'li').length).toEqual(nameListLen());
expect(DOM.querySelectorAll(aboutDOMEl, 'li')[4].textContent).toEqual('Minko');
});
}));
});
}
@Component({
providers: [NameList],
selector: 'test-cmp',
template: '<about></about>',
directives: [AboutCmp]
})
class TestComponent {}
我遇到的问题是,在运行构建系统(使用gulp)之后,所有传输的文件和css都被移动到项目根目录下的另一个文件夹:/dist/dev/所有文件都被移动到这里。
移动文件后的示例结构:
dist/dev/about/components/about.js
dist/dev/about/components/about.html
dist/dev/about/components/about.css
这是我得到的警告和错误:
14 03 2016 16:55:26.687:WARN [web-server]: 404: /base/dist/dev/about/components/about.html
和
SUMMARY:
✔ 1 test completed
✖ 3 tests failed
FAILED TESTS:
About component
✖ should work
PhantomJS 2.1.1 (Mac OS X 0.0.0)
Failed: Failed to load /Users/ogran83/Developer/Projects/angular2-seed/dist/dev/about/components/about.html
run@/Users/ogran83/Developer/Projects/angular2-seed/node_modules/zone.js/dist/zone-microtask.js:1217:29
zoneBoundFn@/Users/ogran83/Developer/Projects/angular2-seed/node_modules/zone.js/dist/zone-microtask.js:1194:29
lib$es6$promise$$internal$$tryCatch@/Users/ogran83/Developer/Projects/angular2-seed/node_modules/zone.js/dist/zone-microtask.js:442:25
lib$es6$promise$$internal$$invokeCallback@/Users/ogran83/Developer/Projects/angular2-seed/node_modules/zone.js/dist/zone-microtask.js:454:53
lib$es6$promise$$internal$$publish@/Users/ogran83/Developer/Projects/angular2-seed/node_modules/zone.js/dist/zone-microtask.js:425:53
lib$es6$promise$$internal$$publishRejection@/Users/ogran83/Developer/Projects/angular2-seed/node_modules/zone.js/dist/zone-microtask.js:375:42
/Users/ogran83/Developer/Projects/angular2-seed/node_modules/zone.js/dist/zone-microtask.js:97:12
run@/Users/ogran83/Developer/Projects/angular2-seed/node_modules/zone.js/dist/zone-microtask.js:1217:29
zoneBoundFn@/Users/ogran83/Developer/Projects/angular2-seed/node_modules/zone.js/dist/zone-microtask.js:1194:29
lib$es6$promise$asap$$flush@/Users/ogran83/Developer/Projects/angular2-seed/node_modules/zone.js/dist/zone-microtask.js:236:18
文件/Users/ogran83/Developer/Projects/angular2 seed/dist/dev/about/components/about。html显然存在,但在PhantomJS中被业力运行时似乎不起作用。在浏览器中测试时,应用程序本身运行良好。
这是一个众所周知的问题
当组件具有templateUrl时,TestComponentBuilder/inject不工作https://github.com/angular/angular/issues/5662
简而言之, > 我正在创建一个项目,从Angular 2“Routing”示例的Plunker代码开始(通过Angular 2 Advanced Tutorial for Routing链接访问)。那个源头没有业力或茉莉花的配置。 然后我运行npm测试,但发现我没有业力能力。从那以后,我根据程序员博客的建议,安装了各种软件包。我的安装变成了: 我运行了“Karma Init”并尽我所能地回答了问题
我正在使用jasmine的karma,并按照在线指南安装了 和其他必需品 我跑了 和 它打开了一个外部铬浏览器,显示因果报应是相关的。我为一个函数写了一个简单的单元测试,它似乎没有运行任何测试 这是我的karma配置文件。 我的单元测试 我要测试的控制器中的特定功能 当我运行因果报应时,控制台上显示的是什么 附加信息:我正在使用AngularJS和RubyonRails。我知道有茉莉宝石可以帮助我
我试图用codeception运行一些测试,当我尝试测试表单时,总是失败,我不知道为什么。和其他人一样,他们都通过了。测试代码如下: 当我用命令运行测试时/vendor/bin/run-vvv--html--debug我有以下日志: 接受。一套yml就是这个: 演员:验收员 模块: 什么是错的?我想要的是测试表格,看看下一页写的假期,为什么一些测试通过了,一个简单的表格没有?。你能帮我个忙吗?
问题内容: 到目前为止,我有两个测试。一个仅使用jUnit框架即可正常工作。另一个使用spring-test库并在每次尝试运行该异常时创建此异常。有什么想法可能导致问题吗? Error Maven test dependencies Dependency tree 问题答案: 你是否正在使用旧版本的Eclipse(Galileo或更低版本)?还是旧版的junit插件?如果是这样,则可能是问题的原因
问题内容: 我有一个已定义的AngularJS指令。我正在尝试与Jasmine进行单元测试。 根据此建议,我的JasmineJavaScript如下所示: 当我在Jasmine spec错误中运行此命令时,出现以下错误: 我想要的只是让templateUrl原样加载- 我不想使用。我相信这可能与使用ngMock而不是ngMockE2E有关。如果是罪魁祸首,如何使用后者而不是前者? 提前致谢! 问题
我一直在想方设法为angular2、sass和ng2引导建立一个基础项目,该项目基于angular2种子项目,您可以在这里找到整个代码库:https://github.com/omargon/angular-seed-sass-ng2-bootstrap对于dev和prod发行版,所有内容都按预期进行了构建。但是,每当我尝试运行uni和e2e测试时,总是会出现以下错误: 欢迎任何帮助。