最近我们开始在Angular 4中重写我们的应用程序。我的任务是专注于使用茉莉和业力进行单元测试。我对这个框架和测试跑步者来说是全新的,主要是对整个战线,所以如果我错过了一些非常重要的东西 - 抱歉。所以我的问题是测试html输出。当我运行我的测试时,我通常得到一些关于哪些失败以及发生了什么样的失败的信息 - 这很好。因为我正在进行测试,最终使一些最简单的工作。现在我开始研究需要一些简单模拟的组件活动。所以这是我的规格
import { WelcomeComponent } from "app/core/welcome/welcome.component";
import { ComponentFixture } from "@angular/core/testing";
import { TestBed } from "@angular/core/testing";
import { async } from "@angular/core/testing";
import { BusinessAreasService } from "app/core/services/business-areas/business-areas.service";
import { CoreModule } from "app/core/core.module";
import { Router } from "@angular/router";
import { DebugElement } from "@angular/core/src/debug/debug_node";
import { By } from "@angular/platform-browser";
describe('WelcomeComponent testing', () => {
let component: WelcomeComponent;
let fixture: ComponentFixture<WelcomeComponent>;
let de: DebugElement;
let el: HTMLElement;
let mockBusinessAreasService;
let mockRouter;
beforeEach(async(() => {
mockBusinessAreasService = {};
mockRouter = {};
TestBed.configureTestingModule({
providers: [
{
provide: BusinessAreasService, useValue: mockBusinessAreasService
},
{
provide: Router, useValue: mockRouter
}
],
declarations: [WelcomeComponent]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(WelcomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.ngOnInit();
mockBusinessAreasService = TestBed.get(BusinessAreasService);
mockRouter = TestBed.get(Router);
});
it('BA list shouldnt be empty'), () => {
expect(component.baList.length).toBeGreaterThan(0);
}
it('check if WelcomeComponent is created', () => {
expect(component).toBeDefined();
});
});
现在我的.ts文件包含WelcomeComponent:
import { Component, OnInit } from '@angular/core';
import { BusinessAreasService } from "app/core/services/business-areas/business-areas.service";
import { Router } from "@angular/router/";
@Component({
selector: 'app-welcome',
templateUrl: './welcome.component.html',
styleUrls: ['./welcome.component.css']
})
export class WelcomeComponent implements OnInit {
baList : string[] = [];
selectedBa : string = "";
constructor(private businessAreasService : BusinessAreasService, private router: Router) { }
ngOnInit() {
console.log("In welcome component")
this.businessAreasService.getUserBAList().subscribe(resposne => this.redirectBasedOnBas(resposne), error => this.baList = [ 'error' ]);
}
redirectBasedOnBas(baList : string[]) {
if(baList.length === 1) {
this.applyBusinessArea(baList[0]);
} else {
this.baList = baList;
this.selectedBa = baList[0];
}
}
baChanged(ba : string) {
console.log(ba);
this.selectedBa = ba;
}
applyBusinessArea(ba : string) {
localStorage.setItem('ba', ba);
this.router.navigate(['']);
}
}
这是我的HTML文件:
<div class="container-fluid">
<div class="col-sm-2">
<label for="name" class="cols-sm-2 control-label">Please select your business Area</label>
</div>
<form class="form-inline text-md-center">
<div class="col-sm-6">
<div class="input-group">
<select class="form-control" id="exampleSelect1" (change)="baChanged($event.target.value)">
<option *ngFor="let ba of baList" [value]="ba">{{ba}} </option>
</select>
</div>
</div>
<div class="col-sm-3">
<button type="button" class="btn btn-primary btn-block" (click)="applyBusinessArea(selectedBa)">Submit</button>
</div>
</form>
</div>
这是我的核心模块,我们没有任何专用的欢迎窗口模块,
import { NgModule, Optional, SkipSelf, OnInit } from '@angular/core';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { EnvironmentService } from "app/core/services/environment/environment.service";
import { HttpAdapterService } from "app/core/services/http-adapter/http-adapter.service";
import { AuthentificationService } from "app/core/services/authentification/authentification.service";
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from "@angular/forms";
import { WelcomeComponent } from './welcome/welcome.component';
import { BusinessAreasService } from "app/core/services/business-areas/business-areas.service";
import { FakeLoginComponent } from "app/core/fake-login/fake-login.component";
import { SharedModule } from "app/shared/shared.module";
import { SystemSettingsService } from "app/core/services/system-settings/system-settings.service";
const SERVICES = [EnvironmentService, HttpAdapterService, AuthentificationService, BusinessAreasService, SystemSettingsService];
const COMPONENTS = [ FakeLoginComponent ];
@NgModule({
imports: [RouterModule, BrowserModule, FormsModule, SharedModule],
providers: [SERVICES],
declarations: [ WelcomeComponent, COMPONENTS ],
exports: [HttpModule, COMPONENTS]
})
class CoreModule implements OnInit {
constructor( @Optional() @SkipSelf() parentModule: CoreModule) {
if (parentModule) {
throw new Error('CoreModule has been already loaded! It should be loaded only once!');
}
console.log('CoreModule contruct');
}
public ngOnInit(): void {
console.log('CoreModule init');
}
}
export { CoreModule };
所以问题出在这里 - 当我运行测试时,我应该在浏览器中得到失败的堆栈/正输出,我得到的是:
我打赌问题在于我的配置测试模块,因为它没有显示欢迎文件的html输出,而是堆栈跟踪失败。但是我想要的只是让这个测试通过。有人可以帮帮我吗?
答案 0 :(得分:0)
在你的测试中你运行了“WelcomeComponent”的ngOnit,你调用了一个一直到这个方法的函数:
applyBusinessArea(ba : string) {
localStorage.setItem('ba', ba);
this.router.navigate(['']); }
现在问题出在这里,你在测试中唠叨''那里的东西得到了展示。