将接口更改为 类 的模型时测试失败

test broke when changed model with interfaces to classes

对不起,如果它有点长,但我想好好解释一下。

我有一个 api 呼叫测试,我正在从我的服务执行接收一个简单的 json。 (Karma-jasmin/angular2)

这是我的服务电话:

@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 我看起来像这样(模拟的一部分):

   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();
        }
      );
    }));

  });

好的,这行得通!问题是当我从这个

更改我的模型时
interface:

    export interface Car {
      make:string;
      year:number;
    }

对此class:

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 })]

所以问题很明显,但是我该如何解决这个问题,我的意思是对于当前的更改,更改该测试的正确方法是什么?

非常感谢那些幸存下来的人:)

toEqual 期望 carsExpectedResponse() 数组元素是某些 class 的实例(例如,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 内部逻辑,也不依赖于构造函数逻辑。