【问题标题】:Angular 11 - Test case writing - Jasmine - deprecation issueAngular 11 - 测试用例编写 - Jasmine - 弃用问题
【发布时间】:2021-08-02 11:40:05
【问题描述】:

我正在尝试使用 Jasmine 创建我的第一个测试用例,这是我第一次尝试编写测试用例(实际上刚刚开始学习)。我目前正在参考This site 编写测试用例

这里我面临两个问题。

  1. 'TestBed.get' 的签名 '(token: any, notFoundValue?: any): any' 已弃用.ts(6387) testing.d.ts(382, 9):声明在此处被标记为已弃用。 (方法) TestBedStatic.get(token: any, notFoundValue?: any): any (+1 重载)

  2. '(typeof EmployeeService)[]' 类型的参数不能分配给 'Type | 类型的参数抽象类型 |注入令牌'。 类型“(typeof EmployeeService)[]”缺少类型“AbstractType”中的以下属性:prototype、apply、call、bind 和 4 多个.ts(2345)

员工模型

export interface Employee {
    name: string;
    desc: null | string;
    action: string;
    obj: Obj;
    action: any;

}

export interface Obj {
    empId: string;
    empName: string;
    reportTo: string;
    country: string;
    region: string;
    empStatus: string;
    department: string;
}

测试用例

describe('DataAccessService', () => {
    let httpTestingController: HttpTestingController;
    let dataAccessService: EmployeeService;
    // let baseUrl = 'czm/getCustomerList';
    let traveller: Employee;
  
    beforeEach(() => {
      TestBed.configureTestingModule({
        imports: [HttpClientTestingModule]
      });
  
      httpTestingController = TestBed.get(HttpTestingController);
      traveller = {
        name: 'Jane',
        desc: 'Jane',
        action: '',
        obj: {
          empId: 'Jane',
          empName: 'Jane',
          reportTo: 'TD1',
          country: 'USA',
          region: 'USA',
          empStatus: 'Inactive',
          department: 'HR - Marketing',
        }
      };
    });
  
    beforeEach(inject(
      [EmployeeService],
      (service: EmployeeService) => {
        dataAccessService = service;
      }
    ));
  });

谁能告诉我如何解决这个问题?

如果可能,我请求一些 茉莉花专家 来创建 stackblitz

谢谢大家

【问题讨论】:

    标签: angular unit-testing types jasmine karma-jasmine


    【解决方案1】:

    TestBed.get 已弃用,请参阅this answer。从 Angular 9 开始,只需使用 TestBed.inject 而不是 TestBed.get

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 2018-01-22
      相关资源
      最近更新 更多