简体   繁体   English

如何在 Angular 中对通用组件进行单元测试

[英]How to unit test a generic component in Angular

I am writing a test for AppleComponent, and which has type <T,U extends BananaComponent<T>> .我正在为 AppleComponent 编写一个测试,它的类型为<T,U extends BananaComponent<T>> And there is BananaComponent<T>还有BananaComponent<T>

target component目标组件

export class AppleComponent<T,U extends BananaComponent<T>>{
   public appleA = 'appleA';
   public appleB = 'appleB';
   public appleC !: U;

}

extended component扩展组件

export abstract class BananaComponent<T> implements OnInit{
   public bananaA = 'bananaA';
   public bananaB = 'bananaB';
}

Here is my spec file for testing AppleComponent这是我用于测试AppleComponent规范文件

import { CommonModule } from '@angular/common';
import { ComponentFixture, TestBed } from '@angular/core/testing';

import {AppleComponent} from '....';
import {BananaComponent} from '....';

describe('AppleComponent', () => {
   let component: AppleComponent;
   let fixture: ComponentFixture<AppleComponent>;

   beforeEach(()=>{
     TestBed.configureTestingModule({
         declarations:[AppleComponent],
         imports:[BananaComponent],
     });
     fixture = TestBed.creatComponent(AppleComponent);
     component = fixture.componentInstance;
   });

   it('should with defaults',() => {
      expect(component.appleA).toBe('appleA','appleA has appleA as default value'); // passes
      expect(component.bananaA).toBe('bananaA','bananaA has bananaA as default value'); // failes
   });
});

Therefore I have a problem with the following code, because component.因此我对以下代码有疑问,因为component. could not obtain any variable or function inside BananaComponent.无法在 BananaComponent 中获取任何变量或 function。

expect(component.bananaA).toBe('bananaA','bananaA has bananaA as default value');

My logic is the types of component and fixture are wrong, but I don't know how to fix it.我的逻辑是组件和夹具的类型是错误的,但我不知道如何修复它。

let component: AppleComponent;
let fixture: ComponentFixture<AppleComponent>;

My question is:我的问题是:

How to correct the code so that component.如何更正该组件的代码。 could obtain the variables and functions inside BananaComponent?可以获取 BananaComponent 里面的变量和函数吗?

Did you try to give the type of your component during the creation?您是否尝试在创建过程中提供组件的类型?

fixture = TestBed.createComponent<AppleComponent<TestObject>>(AppleComponent)

You should probably create a TestObject Model won't work with generic Type T.您可能应该创建一个 TestObject Model 不适用于通用类型 T。

export class TestObject {

}

My misunderstanding about this concept is:我对这个概念的误解是:

AppleComponent is not really extending from BananaComponent, therefore component. AppleComponent 并不是真正从 BananaComponent 扩展而来,因此是组件。 will never be able to obtain anything from BananaComponent.永远无法从 BananaComponent 获得任何东西。

What is happnening here is just the type of AppleComponent extends the BananaComponent;这里发生的只是 AppleComponent 的类型扩展了 BananaComponent; The purpose is to keep the type of the input value inside the AppleComponent to be identical with the type of input value inside BananaComponent.目的是保持 AppleComponent 内部输入值的类型与 BananaComponent 内部输入值的类型相同。

Therefore during the test it is still need to write the component, fixture in a right way.因此在测试过程中仍然需要以正确的方式编写组件、夹具。

Solution for my quesiton I asked我问的问题的解决方案

// first inside the test to define a random type, then give it to AppleComponent, and BananaComponent.

type typeA = { };  
type typeModel = AppleComponent<typeA, BananaComponent<typeA>>;   

let component: typeModel; 
let fixture: ComponentFixture<typeModel>

And for a better understanding of the relationship between the types of AppleComponent and BananaComponent, here are the more detailed codes.为了更好地理解 AppleComponent 和 BananaComponent 的类型之间的关系,这里有更详细的代码。

AppleComponent苹果组件

export class AppleComponent<T,U extends BananaComponent<T>>{
   public appleA = 'appleA';
   public appleB = 'appleB';
   public applePurchase !: U;
}

BananaComponent香蕉组件

export abstract class BananaComponent<T> implements OnInit{
   public bananaA = 'bananaA';
   public bananaB = 'bananaB';
   public bananaPurchase : T;

   public totalPurchase: T;
}

from the above code, we could see that从上面的代码中,我们可以看到

  1. inside BananaComponent, type T is to make sure the input bananaPurchase has the same type with totalPurchase, eg when bananaPurchase = 1000;在 BananaComponent 中,type T 是为了确保输入的bananaPurchase 与totalPurchase 的类型相同,例如当bananaPurchase = 1000 时; then the type is set to number, then the value of totalPurchase has to be a number as well, and vice versa.那么类型设置为数字,那么 totalPurchase 的值也必须是数字,反之亦然。

  2. same situation inside AppleComponent, because it extends the type from BananaComponent, therefore it means the applePurchase should have the same type with bananaPurchase and totalPurchase. AppleComponent 内部的情况相同,因为它扩展了 BananaComponent 的类型,因此这意味着 applePurchase 应该与bananaPurchase 和 totalPurchase 具有相同的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM