【发布时间】:2020-02-13 01:09:23
【问题描述】:
我正在为 AppleComponent 编写一个测试,其类型为 <T,U extends BananaComponent<T>>。还有BananaComponent<T>
目标组件
export class AppleComponent<T,U extends BananaComponent<T>>{
public appleA = 'appleA';
public appleB = 'appleB';
public appleC !: U;
}
扩展组件
export abstract class BananaComponent<T> implements OnInit{
public bananaA = 'bananaA';
public bananaB = 'bananaB';
}
这是我的 spec 文件,用于测试 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
});
});
因此我的规范文件有问题,无法编译。
我的逻辑是组件和夹具的类型错误,我想我需要对组件和夹具进行一些更改,然后得出我在下面写的答案。
// 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>
【问题讨论】:
-
测试中的AppleComponent实际上并没有扩展Banana组件,那么它如何使用香蕉组件的属性。
-
是的,谢谢,这是有道理的,我只是误解了整个上下文。但是如果我需要进行测试,我需要以正确的方式编写组件,夹具。例如类型类型 A = { };类型 typeModel = AppleComponent
>;让组件:typeModel;让夹具:ComponentFixture -
您的查询已解决?
-
是的,解决了
标签: angular typescript unit-testing