角度2测试 - 无法创建组件

时间:2017-01-05 21:22:28

标签: unit-testing angular testing jasmine

我的应用程序包含许多模块,包含许多组件和服务以及其他Angular2内容。

现在我正在尝试使用TestBed方法创建一个使用jasmine + karma的单元测试。

在揭示概念证明时我遇到了错误。 我为我的一个组件创建了一个测试,如下所示:

let myCompServiceStub = {} as MyComponentService;
let routerStub = {} as Router;
let globalServiceStub  = {} as MyGlobalService;

describe('MyComponent', () => {
    let component: MyComponent;
    let fixture: ComponentFixture<MyComponent>;

    beforeEach(async(() => {
        TestBed
            .configureTestingModule({
                imports: [MyModule],
                providers: [
                    { provide: MyComponentService, useValue: myCompServiceStub },
                    { provide: Router, useValue: routerStub },
                    { provide: MyGlobalService, useValue: globalServiceStub }
                ]
            })
            .compileComponents()
            .then(() => {
                fixture = TestBed.createComponent(MyComponent);
                // component = fixture.componentInstance;
            });
    }));


    it('test it', () => {
        expect(true).toBe(true);
    });
});

MyModule有很多其他模块的导入(其中大多数是我的,但是还有CommonModule, MaterializeModule, FormsModule模块形成Angular和Materialize),它们定义了一些全局组件。它在提供者中也有MyComponentService。 导入的自定义模块没有提供的服务。

MyGlobalService中提供了

AppComponent,这是主要内容。

当我尝试运行测试时,出现错误:

PhantomJS 2.1.1 (Windows 8 0.0.0) MyComponent test it FAILED
        invokeTask@karma-shim.js:8833:41
        onInvokeTask@karma-shim.js:10715:50
        invokeTask@karma-shim.js:8832:53
        runTask@karma-shim.js:8709:58
        drainMicroTaskQueue@karma-shim.js:8976:43
        run@karma-shim.js:4098:31
        karma-shim.js:4111:33
        flush@karma-shim.js:4466:13
        resolvePromise@karma-shim.js:9045:79
        karma-shim.js:9081:32

我真的坚持下去,没有好主意我做错了什么?

我的测试依赖项如下:

"@angular/common": "~2.3.0",
"@angular/compiler": "~2.3.0",
"@angular/compiler-cli": "^2.3.1",
"@angular/core": "~2.3.0",
"@angular/forms": "~2.3.0",
"@angular/http": "~2.3.0",
"@angular/platform-browser": "~2.3.0",
"@angular/platform-browser-dynamic": "~2.3.0",
"@angular/platform-server": "^2.3.1",
"@angular/router": "~3.3.0",
"angular2-materialize": "6.3.0",
"karma": "1.1.2",
"karma-chrome-launcher": "^1.0.1",
"karma-coverage": "^1.0.0",
"karma-jasmine": "^1.0.2",
"karma-phantomjs-launcher": "^1.0.0",
"karma-remap-istanbul": "0.1.1",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "1.7.0",
"@types/jasmine": "^2.5.36",
"jasmine-core": "^2.3.4",
"jasmine-spec-reporter": "^2.4.0",

2 个答案:

答案 0 :(得分:1)

确保您实际导入您的组件(无论您是否这样做),无法通过

import { MyComponent } from './MyComponent.component';
import { FormsModule }   from '@angular/forms';

同样在你的TestBed.configureTestingModule中将你的组件设置为声明

    TestBed.configureTestingModule({
      declarations: [ MyComponent ],
      imports:[FormsModule ],
      providers:[]
    })
    .compileComponents()
  }));

P.S。我还在所有测试组件上导入FormsModule。

答案 1 :(得分:0)

  let fixture: ComponentFixture<MyComponent>;

  fixture = TestBed.createComponent(ListPRDComponent);

MyComponent!= ListPRDComponent

尝试:

   fixture = TestBed.createComponent(MyComponent);