类型"x"中缺少属性"jasmineMatches" [英] Property 'jasmineMatches' is missing in type 'x'
问题描述
在ng test
预期的输出:我在x项测试中通过/未通过.
Expected output: i get pass/fail on x amount of tests.
实际输出:
ERROR in src/app/todo-data.service.spec.ts(19,45): error TS2345: Argument of type 'Todo[]' is not assignable to parameter of type 'Expected<Observable<Todo[]>>'.
Type 'Todo[]' is not assignable to type 'ObjectContaining<Observable<Todo[]>>'.
Property 'jasmineMatches' is missing in type 'Todo[]'.
在我的todo类中,我没有jasminematches属性,但这可能不是根本问题.
In my todo class i don't have a jasminematches property, but this might not be the root problem.
todo.ts:
export class Todo {
id: number;
title: string = '';
complete: boolean = false;
constructor(values: Object = {}) {
Object.assign(this, values);
}
}
todo-data.service.spec.ts
todo-data.service.spec.ts
import {TestBed, async, inject} from '@angular/core/testing';
import {Todo} from './todo';
import {TodoDataService} from './todo-data.service';
...
describe('#getAllTodos()', () => {
it('should return an empty array by default', inject([TodoDataService],
(service: TodoDataService) => {
expect(service.getAllTodos()).toEqual(<Todo[]>[]);
}));
...
todo-data.service.ts
todo-data.service.ts
import { Injectable } from '@angular/core';
import { Todo } from './todo';
import { ApiService } from './api.service';
import { Observable } from 'rxjs';
...
export class TodoDataService {
constructor(private api: ApiService) { }
getAllTodos(): Observable<Todo[]> {
return this.api.getAllTodos();
}
...
请告知如何开始解构此错误,以便编写正确的测试.
Please advise how to start deconstructing this error in order to write a correct test.
推荐答案
我得出了与上一个海报相同的结论.您最初的测试断言是将待处理的Http请求(表示为可观察的流和一个可观察的流)与空"Todo []"数组的预期响应进行比较.
I came to the same conclusion of the previous poster. Your initial test assertion is comparing a pending Http request (represented as an observable stream with a single observable) to the expected response of an empty `Todo[]' array.
在下面的代码片段中,.subscribe
方法激活可观察的流.将其放置在fakeAsync
区域内可以模拟时间的流逝,其方式与异步Http
请求的行为相同.由于测试断言位于subscribe
块内部,因此您现在正在处理一个完整的可观察流,并且能够访问该流的内容,即Todo[]
.
In the code snippet below, the .subscribe
method activates the observable stream. Placing it inside of a fakeAsync
zone simulates the passage of time in the same way that an asynchronous Http
request behaves. Since the test assertion is inside the subscribe
block, you are now dealing with a completed observable stream and are able to access the contents of the stream, i.e., Todo[]
.
it('should return an empty array by default', fakseAsync(inject([TodoDataService],
(service: TodoDataService) => {
service.getAllTodos().subscribe(success => {
expect(success).toEqual(<Todo[]>)
});
))
);
我不太了解的测试行为是您的TodoService
似乎将Http
处理委托给了返回Observable<Todo[]>
的ApiService
,而您的TodoService
也返回了Observable<Todo[]>
.如果这两个服务之间存在有意义的抽象层,那么我将使用初始答案中概述的MockApiService
进行测试.
One behavior in your test that I don't quite understand is your TodoService
appears to delegate Http
handling to an ApiService
that returns Observable<Todo[]>
and your TodoService
also returns Observable<Todo[]>
. If there's a meaningful layer of abstraction between these two services, then I'd defer to the testing method with a MockApiService
outlined in the initial answer.
或者,您可以将ApiService
和TodoService
折叠到一个级别中,并对预期的行为进行其他断言(测试/模拟Http
后端的机制取决于您是否使用@angular/http
或@angular/common/http
中的方法.)
Alternatively, you could collapse your ApiService
and TodoService
into a single level and make additional assertions about the expected behavior (the mechanics of testing/simulating the Http
backend will depend on whether you are using the Http
methods in @angular/http
or @angular/common/http
).
这篇关于类型"x"中缺少属性"jasmineMatches"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!