Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2 CLI Karma单元测试失败_Angular_Typescript_Karma Jasmine_Angular Cli - Fatal编程技术网

Angular2 CLI Karma单元测试失败

Angular2 CLI Karma单元测试失败,angular,typescript,karma-jasmine,angular-cli,Angular,Typescript,Karma Jasmine,Angular Cli,我在为正在使用Angular2 CLI测试的组件运行karma测试时遇到问题。我有一个组件,它使用模块中的元素设置,因此我认为这是构建测试的合理方式(见下文)。在TestBed配置中,我添加了我的模块,该模块还包含要测试的组件到imports数组中 /* tslint:disable:no-unused-variable */ import { TestBed, async, ComponentFixture, ComponentFixtureAutoDetect } from '@angul

我在为正在使用Angular2 CLI测试的组件运行karma测试时遇到问题。我有一个组件,它使用模块中的元素设置,因此我认为这是构建测试的合理方式(见下文)。在TestBed配置中,我添加了我的模块,该模块还包含要测试的组件到imports数组中

/* tslint:disable:no-unused-variable */

import { TestBed, async, ComponentFixture, ComponentFixtureAutoDetect } from '@angular/core/testing';
import { MyBtnComponent } from './';
import { MyModule } from '../../my-module.module';

let fixture : ComponentFixture<MyBtnComponent>;
let btn  : MyBtnComponent;

describe('Component: MyBtn', () => {
    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [
                MyModule
            ]
        }).compileComponents();

        fixture = TestBed.createComponent(MyBtnComponent);
        btn = fixture.debugElement.componentInstance;
    }));

    it('should create the button component', async(() => {
        expect(btn).toBeTruthy();
    }));

});
my-module.module.ts

import { NgModule, ModuleWithProviders } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { MyBtnComponent } from './_components/my-btn/my-btn.component';

@NgModule({
    imports: [
        NgbModule.forRoot(),
        BrowserModule,
        CommonModule,
        FormsModule
    ],
    declarations: [
        MyBtnComponent
    ],
    exports: [
        MyBtnComponent
    ]
})
export class MyModule {
}

我通过重组我的应用程序解决了这个问题。我认为我遇到的主要问题是karma runner与编译器的配置不太匹配。

我通过重组应用程序解决了这个问题。我认为我遇到的主要问题是karma runner与编译器的配置不匹配。

…那么模块在哪里?请给出一个。此处不需要
ModuleWithProviders
<代码>浏览器模块也不需要,因为您有
公共模块
什么是
NgbModule.forRoot(),
?您似乎没有在任何地方导入它。NgbModule是引导模块。MyBtnComponent使用了其中的元素。我认为发生的情况是,在运行karma测试时,模块没有导入MyBtnComponent。在终端中注销后,将显示此未定义。是否有更好的导入方法,或者这更像是测试设置的配置问题?…那么模块在哪里?请给出一个。此处不需要
ModuleWithProviders
<代码>浏览器模块也不需要,因为您有
公共模块
什么是
NgbModule.forRoot(),
?您似乎没有在任何地方导入它。NgbModule是引导模块。MyBtnComponent使用了其中的元素。我认为发生的情况是,在运行karma测试时,模块没有导入MyBtnComponent。在终端中注销后,将显示此未定义。是否有更好的导入方法,或者这更像是测试设置的配置问题?我也看到了这个问题。你能描述一下你是如何重组你的应用程序的吗?你做了什么?我也看到了这个问题。你介意描述一下你是如何重组你的应用程序的吗?你做了什么?
{
  "name": "my-project",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^2.3.1",
    "@angular/compiler": "^2.3.1",
    "@angular/core": "^2.3.1",
    "@angular/forms": "^2.3.1",
    "@angular/http": "^2.3.1",
    "@angular/platform-browser": "^2.3.1",
    "@angular/platform-browser-dynamic": "^2.3.1",
    "@angular/router": "^3.3.1",
    "@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.15",
    "core-js": "^2.4.1",
    "rxjs": "^5.0.1",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.7.2"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^2.3.1",
    "@types/jasmine": "2.5.38",
    "@types/node": "^6.0.42",
    "angular-cli": "1.0.0-beta.24",
    "bootstrap": "^4.0.0-alpha.5",
    "codelyzer": "~2.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "~4.0.13",
    "ts-node": "1.2.1",
    "tslint": "^4.0.2",
    "typescript": "~2.0.3"
  }
}
import { NgModule, ModuleWithProviders } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { MyBtnComponent } from './_components/my-btn/my-btn.component';

@NgModule({
    imports: [
        NgbModule.forRoot(),
        BrowserModule,
        CommonModule,
        FormsModule
    ],
    declarations: [
        MyBtnComponent
    ],
    exports: [
        MyBtnComponent
    ]
})
export class MyModule {
}