angular - Angular 测试失败
问题描述
我正在关注一个视频,其中讨论了异步测试,并且我编写了相同的代码,但由于 Angular 版本(该视频已有 2 年历史)存在一些差异。但是,我遇到了异步测试的问题。
预期未定义等于“testTtess”
import { TtessComponent } from './ttess.component'
import { TestBed, ComponentFixture, tick, async } from '@angular/core/testing';
import { TtessService } from './ttes.service';
import { of } from 'rxjs'
import { delay } from 'rxjs/operators';
describe('TtesComponent', () => {
let fixture: ComponentFixture<TtessComponent>;
let component: TtessComponent;
let ttessService: TtessService;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TtessComponent]
});
fixture = TestBed.createComponent(TtessComponent);
component = fixture.debugElement.componentInstance;
ttessService = fixture.debugElement.injector.get(TtessService);
});
it('should creat component instance', () => {
expect(component).toBeTruthy();
});
it(`it should render h1 tag with title 'My car header'`, () => {
fixture.detectChanges();
const nativeEl = fixture.debugElement.nativeElement;
expect(nativeEl.querySelector('h1').textContent).toEqual('My car header')
})
it('should inject Ttess service', () => {
fixture.detectChanges();
expect(component.isCarVisible).toEqual(ttessService.getVisibility());
})
it('should display car if is not visible', () => {
ttessService.showTtess();
fixture.detectChanges();
const nativeEl = fixture.debugElement.nativeElement;
expect(nativeEl.querySelector('span').textContent).toEqual('Car is visible');
})
it(`shouldn't get car name if not async`, () => {
spyOn(ttessService, 'getTtessName')
.and.returnValue(of('testTtess')
.pipe(delay(100)));
fixture.detectChanges();
expect(component.ttessName).toBe(undefined);
})
it(`should get car name if async`, async(() => {
spyOn(ttessService, 'getTtessName').and.returnValue(of('testTtess').pipe(delay(100)));
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.ttessName).toEqual('testTtess');
});
}));
});
\===========Service===============\
export class TtessService {
private IsCarVisible: boolean = true;
showTtess() {
this.IsCarVisible = true;
}
hideIttess() {
this.IsCarVisible = false;
}
getVisibility() {
return this.IsCarVisible;
}
getTtessName(): Observable<string> {
return of('Ford').pipe(delay(100));
}
}
\===============Component=================\
import { Component, OnInit } from '@angular/core';
import { TtessService } from './ttes.service';
@Component({
selector: 'app-ttess',
templateUrl: './ttess.component.html',
styleUrls: ['./ttess.component.scss'],
providers: [TtessService]
})
export class TtessComponent implements OnInit {
isCarVisible:boolean;
title = 'My car header';
ttessName: string;
constructor(private ttess: TtessService) { }
ngOnInit() {
this.isCarVisible = this.ttess.getVisibility();
this.ttess.getTtessName()
.subscribe(name => this.ttessName);
}
}
解决方案
试试看
ngOnInit() {
this.isCarVisible = this.ttess.getVisibility();
this.ttess.getTtessName()
.subscribe(name => this.ttessName = name);
}
推荐阅读
- android - 无法创建进程:执行流式安装 adb:安装失败
- python - pyspark rdd 将多个 json 文件数据合并到一个 rdd
- pandas - 如何在熊猫的数据框中按行分组
- azure - 从 .NET Core 向 Application Insights 跟踪添加自定义维度
- android - 如何在 Android 上的 EditText 上设置属性和 actionEvent
- javascript - 页面在初始页面加载时有效,但如果我刷新页面,我会收到 Zeit、Nextjs 和现在的 404 错误
- mysql - 无法诊断 MySQL 存储过程的问题
- java - 我的自定义信号量似乎在我的程序中不起作用
- python - 为张量流分割模型获取混淆矩阵的有效方法是什么?
- java - 如何将txt读取到arrayList然后返回arrayList?