gpt4 book ai didi

Angular 单元测试异步与同步问题

转载 作者:行者123 更新时间:2023-11-28 21:19:32 27 4
gpt4 key购买 nike

我对 angular 和 jasmine 的单元测试还很陌生,所以我一直在努力把它做好。我正在尝试为登录页面编写一个简单的单元测试。以下是我的代码

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { LoginComponent } from './login.component';
import {BrowserModule, By} from '@angular/platform-browser';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {Component, DebugElement, Input} from '@angular/core';
import {RouterTestingModule} from '@angular/router/testing';
import {LoaderService} from '@shared/services/loader.service';
import {AuthenticationService} from '@shared/services/authentication.service';
import {HttpClientModule} from '@angular/common/http';
import {CommonService} from '@shared/services/common.service';
import {ExploreService} from '@shared/services/explore.service';
import {TitleService} from '@shared/services/title.service';
import {AppConfig} from '../../app-config.service';
import {ThemeService} from '@shared/services/theme.service';
import {MatDialog, MatFormFieldModule, MatIconModule} from '@angular/material';
import {throwError} from 'rxjs';

@Component({
selector: 'show-errors',
template: '<p>Mock Product Settings Component</p>'
})
class MockShowErrorsComponent {
@Input() public control;
}
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
let authService: any;
let common: any;
let explore: any;
let title: any;
let config: any;
let theme: any;
let dialog: any;
let debugElement: DebugElement;
let element: HTMLElement;
let submitSpy: any;
beforeEach(async(() => {
authService = jasmine.createSpyObj('AuthenticationService', [
'login',
'logout'
]);
common = jasmine.createSpyObj('commonService', [
'updateCurrentUrl',
'isMobile',
]);
explore = jasmine.createSpyObj('exploreService', [
'slowCalcMessage',
'cancelSlowMessage',
'getInventoryTotalSummary',
'handleError',
'getMarketData'
]);
title = jasmine.createSpyObj('titleService', [
'getTitle',
'setTitle',
'updateTitle',
'updateSiteName'
]);
config = jasmine.createSpyObj('AppConfigService', [
'load',
'API_ENDPOINT'
]);
theme = jasmine.createSpyObj('ThemeService', [
'getThemeSettings',
'generateColorTheme',
]);
dialog = jasmine.createSpyObj('dialog', [
'open'
]);
TestBed.configureTestingModule({
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
RouterTestingModule.withRoutes([]),
HttpClientModule,
MatIconModule,
MatFormFieldModule,
],
declarations: [
LoginComponent,
MockShowErrorsComponent
],
providers: [
LoaderService,
{provide: AuthenticationService, useValue: authService},
{provide: CommonService, useValue: common},
{provide: ExploreService, useValue: explore},
{provide: TitleService, useValue: title},
{provide: AppConfig, useValue: config},
{provide: ThemeService, useValue: theme},
{provide: MatDialog, useValue: dialog},
]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
element = debugElement.nativeElement;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
it('should fail on wrong credentials', () => {
const email = element.querySelector('#defaultForm-email') as HTMLInputElement;
email.value = 'vigneshm@intermx.com';
email.dispatchEvent(new Event('input'));
const password = element.querySelector('#defaultForm-pass') as HTMLInputElement;
password.value = 'agira123';
password.dispatchEvent(new Event('input'));
fixture.detectChanges();
const service = debugElement.injector.get(AuthenticationService);
submitSpy = spyOn(service, 'logout');
debugElement.query(By.css('button.login-btn'))
.triggerEventHandler('click', null);
fixture.detectChanges();
expect(submitSpy).toHaveBeenCalled();
});
});

上面的代码是我对登录屏幕的规范和模拟,我意识到登录页面有太多的依赖关系,在开始模拟它们时需要简化它们。

真正的问题是每当我运行它时,第二个测试都会失败并显示 TypeError: Cannot set property 'value' of null

我不确定为什么在测试时DOM不可用,我应该等待DOM或angular准备好吗?还是其他原因?

最佳答案

使用 vsc 并安装 simon 测试插件。现在右键单击要为其编写单元测试的组件。选择选项生成测试。它将生成一个 spec 文件,其中包含所有依赖项。现在你复制粘贴你上面文件的测试用例。

关于 Angular 单元测试异步与同步问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53445205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com