我有以下的代码来调用angular2Angular2测试 “提供=>使用价值”
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
import { AppModule } from "./src/app";
export function runAngular2App(legacyModel: any) {
platformBrowserDynamic([
{ provide: "legacyModel", useValue: model }
]).bootstrapModule(AppModule)
.then(success => console.log("Ng2 Bootstrap success"))
.catch(err => console.error(err));
}
而且某处,我调用它像这样 -
var legacyModel = {}; // some data
require(["myAngular2App"], function(app) {
app.runAngular2App(legacyModel); // Input to your APP
});
header.component。 TS 而在我的部分我用的是传统的模型 -
import { Component, ViewEncapsulation, Inject } from '@angular/core';
@Component({
selector: 'app-header',
encapsulation: ViewEncapsulation.Emulated,
styleUrls: [ './header.less' ],
templateUrl: './header.html'
})
export class HeaderComponent {
public eventTitle;
constructor(@Inject("appModel") private appModel) {
this.eventTitle = this.appModel.get("eventTitle");
}
}
现在的问题是,当我测试这个组件 -
header.component.spec.ts
import {} from 'jasmine';
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { HeaderComponent } from './header.component';
describe('HeaderComponent',() => {
let comp: HeaderComponent;
let fixture: ComponentFixture<HeaderComponent>;
let de: DebugElement;
let el: HTMLElement;
// async beforeEach
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ HeaderComponent ]
})
.compileComponents(); // compile template and css
}));
// synchronous beforeEach
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent);
comp = fixture.componentInstance; // HeaderComponent test instance
de = fixture.debugElement.query(By.css('.title'));
el = de.nativeElement;
});
it('should display event title',() => {
fixture.detectChanges();
expect(el.textContent).toContain(comp.eventTitle);
});
it('should display a different event title',() => {
comp.eventTitle = "Angular2 moderator dashboard";
fixture.detectChanges();
expect(el.textContent).toContain("Angular2 moderator dashboard");
});
});
我碰到下面的错误 -
错误:没有提供程序appModel!在spec.bundle.ts(行4110)
由于appModel
不是服务我不能注入它。
如何注入appModel以便我的测试可以运行?