英文:
Error "Variable is used before being assigned" in an Angular unit test although it's like in an article
问题
以下是代码部分的中文翻译:
我是 Angular 单元测试的新手,尝试测试 API 访问。
这是我的代码:
```typescript
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { JwtHelperService, JWT_OPTIONS } from '@auth0/angular-jwt';
import { AuthService } from '../../services/auth.service';
import { UserService } from '../../services/user.service';
import { WorkService } from '../../services/work.service';
import { ErrorToLogService } from '../../services/errorToLog.service';
import { FooterComponent } from './footer.component';
describe('FooterComponent', () => {
let component: FooterComponent;
let fixture: ComponentFixture<FooterComponent>;
let httpController: HttpTestingController;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [
AuthService,
UserService,
WorkService,
ErrorToLogService,
JwtHelperService,
{ provide: JWT_OPTIONS, useValue: JWT_OPTIONS },
],
declarations: [FooterComponent]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(FooterComponent);
component = fixture.componentInstance;
fixture.detectChanges();
httpController = TestBed.inject(HttpTestingController);
});
afterEach(() => {
TestBed.resetTestingModule();
});
it('应该创建', () => {
expect(component).toBeTruthy();
});
it('检查 API 返回网站管理员', () => {
let res: string;
component.getUserService().getWebmasterEmail().subscribe(
data => res = data,
);
const req = httpController.expectOne({
method: 'GET',
url: `/api/getWebmasterEmail`,
});
req.flush("toto");
expect(res).toEqual("toto");
});
});
测试 检查 API 返回网站管理员
无法编译,因为出现了变量 'res' 在分配之前使用
的错误。
我有点理解,但我看不出为什么它在这篇文章中的“Angular Service Unit Testing Example with HttpClient” 部分(测试“应该返回数据”)中能够更好地工作。
有人能解释这个问题吗?
英文:
I'm new at Angular unit tests and try to test an API access.
Here's my code:
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'
import { JwtHelperService, JWT_OPTIONS } from '@auth0/angular-jwt'
import { AuthService } from '../../services/auth.service'
import { UserService } from '../../services/user.service'
import { WorkService } from '../../services/work.service'
import { ErrorToLogService } from '../../services/errorToLog.service'
import { FooterComponent } from './footer.component'
describe('FooterComponent', () => {
let component: FooterComponent
let fixture: ComponentFixture<FooterComponent>
let httpController: HttpTestingController
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [
AuthService,
UserService,
WorkService,
ErrorToLogService,
JwtHelperService,
{ provide: JWT_OPTIONS, useValue: JWT_OPTIONS },
],
declarations: [ FooterComponent ]
})
.compileComponents()
})
beforeEach(() => {
fixture = TestBed.createComponent(FooterComponent)
component = fixture.componentInstance
fixture.detectChanges()
httpController = TestBed.inject(HttpTestingController)
})
afterEach(() => {
TestBed.resetTestingModule()
})
it('should create', () => {
expect(component).toBeTruthy()
})
it('Check API returns the webmaster', () => {
let res: string
component.getUserService().getWebmasterEmail().subscribe(
data => res = data,
)
const req = httpController.expectOne({
method: 'GET',
url: `/api/getWebmasterEmail`,
})
req.flush("toto")
expect(res).toEqual("toto")
})
})
The test Check API returns the webmaster
doesn't compile because Variable 'res' is used before being assigned
.
I kind of understand but I can't see why it would work better in the paragraph "Angular Service Unit Testing Example with HttpClient" of this article (test "should return data").
Anyone to shed light on this issue?
答案1
得分: 2
req.flush()
表示你正在解决这个 promise。你仍然需要将你的 expect
移到 promise 内部。
it('检查 API 返回网站管理员', () => {
let res: string;
component.getUserService().getWebmasterEmail().subscribe(
data => {
res = data;
expect(res).toEqual("toto");
}
);
const req = httpController.expectOne({
method: 'GET',
url: `/api/getWebmasterEmail`,
});
req.flush("toto");
})
英文:
req.flush()
just means that you are resolving the promise. you'll still need to move your expect
to within the promise.
it('Check API returns the webmaster', () => {
let res: string
component.getUserService().getWebmasterEmail().subscribe(
data => {
res = data,
expect(res).toEqual("toto");
}
)
const req = httpController.expectOne({
method: 'GET',
url: `/api/getWebmasterEmail`,
})
req.flush("toto")
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论