如何为具有继承的类编写测试 [英] How to write tests for classes with inheritance

查看:142
本文介绍了如何为具有继承的类编写测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Angular 1.3编写的项目,它有相互继承的服务。有一个 BaseApiService ,它是一个父类,它实现了许多与API调用相关的功能。它看起来像这样:

I have a project written in Angular 1.3 that have services that inherit each other. There is a BaseApiService, which is a parent class and it implements a lot of functionality related to API calls. It looks like that:

.factory('BaseApiService', function ($resource, apiHost, apiUrl, WebSocketService, $q, $timeout) {

  //initializer
  return function (resourceName, options) {
    options = angular.extend({}, {
      resourceUrl: resourceName + '/:id',
      resourceParams: {id: '@id'},
      resourceMethods: {
        'get':    {method:'GET'},
        'update': {method:'PUT'},
        'save':   {method:'POST'},
        'all':    {method:'GET', isArray:true},
        'remove': {method:'DELETE'},
        'delete': {method:'DELETE'}
      }
    }, options);


    this.loadCache = function () {
      //...
    };

    this.new = function () {
      //...
    };

    this.get = function (id) {
      //...
    };

    this.getBy = function (propertyName, value) {
      //...
    };

    this.all = function (params) {
      //...
    };

    //lots of other methods here

然后有多个子课程实现对特定API端点的访问。它们是这样完成的:

Then there are multiple children classes that implement access to the specific API end-points. They are done like that:

.factory('HandymanTasks', function(BaseApiService, $q){

  var service = new BaseApiService('handyman_tasks');

  angular.extend(service, {
    getByRoomId: function(room_id){
      return service.getBy('room_id', room_id);
    },
    capturePhoto: function() {
      //...
    }
  });

  return service;
});

儿童服务大部分都使用父方法,只添加一些自己的方法。父类只是为了继承而实例化,它不是自己使用的。我的问题是:如何在这种情况下正确编写测试?我需要确保所有API访问函数都适用于所有子类。
我的想法是:

Children services for the most part use parent's methods and only add a few of their own. Parent class is only instantiated for the sake of inheritance, it isn't used on it's own. My question is: how to properly write tests in this scenario? I need to ensure the all the API access functions work in all the children classes. My ideas are:


  • 编写一个帮助程序,测试所有API访问函数(在父代中实现)然后在所有儿童班的考试中称呼它;

  • 在父类中测试API调用,假设它们在子类中工作,因为我知道它们总是被继承。仅测试子类中自己的函数;

  • 测试子项是否定义了父项的所有方法,假设它们正常工作,因为父类被测试覆盖;

基本上,我只想确保子类的所有功能都能正常工作,但没有太多的复制粘贴/在每个班级的测试服中都有非常相似的测试。

Basically, I just want to ensure that all the functionality of the children classes work properly, but don't have too much copy-pasted/very similar tests in a test suit for each class.

推荐答案

我认为您可以使用测试父服务的方法然后只测试特定于子服务的功能/属性。您的基本服务功能实际上只需要测试一次,因为每个孩子都调用相同的功能,因此不需要测试每个功能。

I think you would be fine with the approach of testing the parent service and then only testing the functions/properties specific to the children services. Your Base service functions only really need to be tested once because each child calls to the same functions so testing each of those would be unnecessary.

这篇关于如何为具有继承的类编写测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆