如何使用 angular 中的主题测试服务
How to test Service with Subject in angular
这是我的服务。
import { Injectable } from "@angular/core";
import { Subject } from "rxjs";
import { Observable } from "rxjs";
import { filter, map } from "rxjs/operators";
interface i {
key: any;
value: any;
}
@Injectable()
export class Srvc {
private subject: Subject<i>;
constructor() {
this.subject = new Subject<i>();
}
next(key: any, value?: any) {
this.subject.next({ key, value });
}
sub<T>(key: any): Observable<T> {
return this.subject.asObservable().pipe(
filter((event) => {
return event.key === key;
}),
map((event) => <T>event.value)
);
}
}
这是我的规范文件
import { Observable, of, Subject } from "rxjs";
import { Injectable } from "@angular/core";
import { TestBed, inject } from "@angular/core/testing";
import { Srvc } from "./srvc.service"
interface i{
key: any;
value: any;
}
fdescribe("Srvc", () => {
let service: Srvc;
beforeEach(async () => {
TestBed.configureTestingModule({
providers: [
Srvc,
],
});
});
it("should be created", () => {
service = TestBed.inject(Srvc);
expect(service).toBeTruthy();
})
it('should be subscribed', (() => {
service = TestBed.inject(Srvc);
service.next('test', "Hello World");
service.sub<any>('test').subscribe((message: any) => {
expect(message).toBe("Hello World")
})
}))
})
我收到“SPEC HAS NO EXPECTATIONS should be subscribed”。我开始在 angular 中进行测试。我正在阅读有关它的文章,但无法弄清楚我做错了什么。任何建议都会有所帮助。谢谢。
it('should be subscribed', ((done) => {
service = TestBed.inject(Srvc);
service.sub<any>('test').subscribe((message: any) => {
expect(message).toBe("Hello World");
done();
})
service.next('test', "Hello World");
}))
它是异步的,所以测试套件不知道异步部分是什么时候 'done'
这是我的服务。
import { Injectable } from "@angular/core";
import { Subject } from "rxjs";
import { Observable } from "rxjs";
import { filter, map } from "rxjs/operators";
interface i {
key: any;
value: any;
}
@Injectable()
export class Srvc {
private subject: Subject<i>;
constructor() {
this.subject = new Subject<i>();
}
next(key: any, value?: any) {
this.subject.next({ key, value });
}
sub<T>(key: any): Observable<T> {
return this.subject.asObservable().pipe(
filter((event) => {
return event.key === key;
}),
map((event) => <T>event.value)
);
}
}
这是我的规范文件
import { Observable, of, Subject } from "rxjs";
import { Injectable } from "@angular/core";
import { TestBed, inject } from "@angular/core/testing";
import { Srvc } from "./srvc.service"
interface i{
key: any;
value: any;
}
fdescribe("Srvc", () => {
let service: Srvc;
beforeEach(async () => {
TestBed.configureTestingModule({
providers: [
Srvc,
],
});
});
it("should be created", () => {
service = TestBed.inject(Srvc);
expect(service).toBeTruthy();
})
it('should be subscribed', (() => {
service = TestBed.inject(Srvc);
service.next('test', "Hello World");
service.sub<any>('test').subscribe((message: any) => {
expect(message).toBe("Hello World")
})
}))
})
我收到“SPEC HAS NO EXPECTATIONS should be subscribed”。我开始在 angular 中进行测试。我正在阅读有关它的文章,但无法弄清楚我做错了什么。任何建议都会有所帮助。谢谢。
it('should be subscribed', ((done) => {
service = TestBed.inject(Srvc);
service.sub<any>('test').subscribe((message: any) => {
expect(message).toBe("Hello World");
done();
})
service.next('test', "Hello World");
}))
它是异步的,所以测试套件不知道异步部分是什么时候 'done'