很抱歉,如果它有点长,但我想自己解释一下。
我有一个api调用的测试我从我的服务执行以接收一个简单的json。 (噶-茉莉/ angular2)
这是来自我服务的电话:
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;
}
汽车界面是:
status: 200,
headers: new Headers(HEADERS),
body: {
cars: [
{
"make": "Mercedes",
"year": 2016
},
{
"make": "Audi",
"year": 2017
},
{
"make": "BMW",
"year": 2015
}
]
}
json我看起来像这样(模拟的一部分):
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();
}
);
}));
});
:
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;
}
到这个班级:
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 })]
所以现在我得到的错误是:
$ host kms.us-west-1.amazonaws.com
kms.us-west-1.amazonaws.com has address 176.32.112.55
$ host 176.32.112.55
Host 55.112.32.176.in-addr.arpa. not found: 3(NXDOMAIN)
所以问题非常明显,但我如何解决这个问题,我的意思是当前的改变是改变测试的正确方法?
感谢一堆幸存到现在的人:)
答案 0 :(得分:1)
toEqual
期望carsExpectedResponse()
个数组元素是某个类的实例(例如,Car
)。它tests object constructors并期望它们具有包含构造函数的非枚举属性constructor
。
它可以是一个真实的例子:
let carsExpectedResponse = () => [
new Car(...),
...
];
它可以是假的不可枚举的constructor
属性:
let carsExpectedResponse = () => [
{
"make": "Mercedes",
"year": 2016
},
...
].map(obj => Object.defineProperty(obj, 'constructor', { value: Car }));
它可以是用所需原型链构建的对象:
let carsExpectedResponse = () => [
{
"make": "Mercedes",
"year": 2016
},
...
].map(obj => Object.assign(Object.create(Car.prototype), obj));
最后一个fixture对象可能是最坚固的,因为它不依赖于Jasmine内部逻辑,也不依赖于构造函数逻辑。