使用类接口更改模型时测试中断 [英] test broke when changed model with interfaces to classes
问题描述
很抱歉,如果它有点长,但我想自己解释一下。
Sorry if its a bit long but I wanted to explain myself well.
我有一个api调用的测试即时通过我的服务执行以接收一个简单的json 。 (Karma-jasmin / angular2)
I have a test for an api call im performing from my service to receive a simple json. (Karma-jasmin/angular2)
这是来自我服务的电话:
this is the call from my service:
@Injectable()
export class MyService {
constructor(private _myApiService:MyAPIService) {
}
public getCarsData():Observable<Car[]> {
return this._myApiService.getCurrentCarData().map(res => res.carList);
}
}
这是 getCurrentCarData()
:
export interface CarsListResponse extends ServerResponse {
carList: Cars[];
}
public getCurrentCarData(): Observable<CarsListResponse> {
let apiURL = 'my/api/url'
return this.http.get(apiURL),
(ret, retJson) => ret.status === 200 ? {carList: retJson.cars.map(element => new Car(element.year, element.make))} : undefined);
}
汽车界面是:
export interface Car {
make:string;
year:number;
}
json我看起来像这样(模拟的一部分):
the json Im getting looks like this (part of a mock):
status: 200,
headers: new Headers(HEADERS),
body: {
cars: [
{
"make": "Mercedes",
"year": 2016
},
{
"make": "Audi",
"year": 2017
},
{
"make": "BMW",
"year": 2015
}
]
}
测试:测试是针对 getCurrentCarData( )
:
let carsResponse = () => {
return { cars: [
{
"make": "Mercedes",
"year": 2016
},
{
"make": "Audi",
"year": 2017
},
{
"make": "BMW",
"year": 2015
}
]}
};
let carsExpectedResponse = () => {
return [
{
"make": "Mercedes",
"year": 2016
},
{
"make": "Audi",
"year": 2017
},
{
"make": "BMW",
"year": 2015
}
]
};
describe('GET Car Data', () => {
it('should handle respond', inject([XHRBackend, api.MyApiService], (mock, myApiService) => {
let c:Connection = null;
mock.connections.subscribe((connection) => {
connection.mock(new Response(new ResponseOptions({
status: 200,
headers: jsoHeaders(),
body: carsResponse()
})));
c = connection;
});
myApiService.getCurrentCarData().subscribe(
res => {
expect(c.request.url).toEqual(`my/api/url`);
expect(res.carList).toEqual(carsExpectedResponse());
},
error => {
expect(false).toBeTruthy();
}
);
}));
});
好的,这样可行!问题是当我从这个改变我的模型时
Ok, so this works! THE problem is when I changed my model from this
interface:
export interface Car {
make:string;
year:number;
}
到这个班级:
export class Car implements GenType {
make:string;
year:number;
constructor(make:string, year:number) {
this.make = make;
this.year = year;
}
displayFormat:() => string = function () {
return 'someStr'
}
}
export interface GenType {
displayFormat: () => string;
}
所以现在我得到的错误是:
so now the error im getting is:
Expected
[MatcherEntityBulk({ displayFormat: Function, make: 'Mercedes', year: 2016 }),
MatcherEntityBulk({ displayFormat: Function, make: 'Audi', year: 2017 }),
MatcherEntityBulk({ displayFormat: Function, make: 'BMW', year: 2015 })]
to equal
[Object({ displayFormat: Function, make: 'Mercedes', year: 2016 }),
Object({ displayFormat: Function, make: 'Audi', year: 2017 }),
Object({ displayFormat: Function, make: 'BMW', year: 2015 })]
所以问题非常明显,但是我如何解决这个问题,我的意思是当前的改变是什么才能改变那个测试的正确方法?
So the problem is pretty obvious, but how do I fix this, I mean with the current change what would be a proper way to change that test?
感谢一堆幸存到这一点: )
thanks a bunch for the ones that survived to this point :)
推荐答案
toEqual
期望 carsExpectedResponse()
数组元素是某个类的实例(例如, Car
)。它测试对象构造函数并期望它们具有包含构造函数的非可枚举属性构造函数
。
toEqual
expects carsExpectedResponse()
array elements to be instances of certain class (for example, Car
). It tests object constructors and expects them to have non-enumerable property constructor
that contains a constructor.
它可以是一个真实的实例:
It can be a real instance:
let carsExpectedResponse = () => [
new Car(...),
...
];
它可以是假的不可枚举的构造函数
property:
It can be a fake non-enumerable constructor
property:
let carsExpectedResponse = () => [
{
"make": "Mercedes",
"year": 2016
},
...
].map(obj => Object.defineProperty(obj, 'constructor', { value: Car }));
它可以是使用所需原型链构建的对象:
It can be an object constructed with desired prototype chain:
let carsExpectedResponse = () => [
{
"make": "Mercedes",
"year": 2016
},
...
].map(obj => Object.assign(Object.create(Car.prototype), obj));
最后一个夹具对象可能是最坚固的,因为它不依赖于Jasmine内部逻辑和也不依赖于构造函数逻辑。
The last fixture object is probably the most solid, because it doesn't depend on Jasmine internal logic and doesn't depend on constructor logic, too.
这篇关于使用类接口更改模型时测试中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!