gpt4 book ai didi

angular - 测试通用 Angular 组件(非通用类)

转载 作者:行者123 更新时间:2023-12-01 21:56:46 24 4
gpt4 key购买 nike

是否可以测试 Angular 通用组件?

举个例子,如果我有

@Component({
...
})
export class MyComponent<T> implements OnInit {
public constructor(
private context: T
);
}

其中 T 可以是任意数量的类。

最后我想写一个 Jasmine 测试,但我无法确定创建测试夹具时的正确语法:

fixture = TestBed.createComponent(MyComponent<MyTestObject>);

TSLint 在这里提示:

Value of type 'typeof MyComponent' is not callable. Did you mean to include new?

它显然认为我需要一个构造函数(?),但不清楚如何或在哪里实现它。

我正在尝试的是可能的吗?我可以找到通用组件的示例。我无法找到关于设置测试夹具/组件的示例

最佳答案

TestBed.createComponent函数本身是通用的并返回一个 componentFixture,所以这是您应该应用通用的地方。对于您的用例,您应该这样做:

fixture = TestBed.createComponent<MyComponent<MyTestObject>>(MyComponent);

记得将您的灯具声明为

let fixture: ComponentFixture<MyComponent<MyTestObject>>;

关于angular - 测试通用 Angular 组件(非通用类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56350489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com