gpt4 book ai didi

angular - 测试方法 setter @Input() Angular 4 Karma

转载 作者:行者123 更新时间:2023-11-28 20:27:38 26 4
gpt4 key购买 nike

如何从组件中测试此方法:

@Input() set camera(camera: CameraModel) {
this._camera = camera;
if (this._camera && this._camera.cameraId) {
this.fetchVideos(this._camera);
}
}

在测试的时候,需要把我的stub给他,但是我没试过,不行...

信息

如果我在测试中使用 setter 作为方法,我会收到错误“component.camera is not a function”

TypeError: component.camera is not a function
at Object.eval (webpack:///D:/dev/nighthawk/frontend/src/app/components/dashboard-videos-list/dashboard-videos-list.component.spec.ts?:86:23)
at ZoneDelegate.invoke (webpack:///D:/dev/nighthawk/frontend/~/zone.js/dist/zone.js?:392:26)
at AsyncTestZoneSpec.onInvoke (webpack:///D:/dev/nighthawk/frontend/~/zone.js/dist/async-test.js?:49:39)
at ProxyZoneSpec.onInvoke (webpack:///D:/dev/nighthawk/frontend/~/zone.js/dist/proxy.js?:76:39)
at ZoneDelegate.invoke (webpack:///D:/dev/nighthawk/frontend/~/zone.js/dist/zone.js?:391:32)
at Zone.runGuarded (webpack:///D:/dev/nighthawk/frontend/~/zone.js/dist/zone.js?:155:47)
at runInTestZone (webpack:///D:/dev/nighthawk/frontend/~/@angular/core/@angular/core/testing.es5.js?:128:25)
at Object.eval (webpack:///D:/dev/nighthawk/frontend/~/@angular/core/@angular/core/testing.es5.js?:67:13)
at ZoneDelegate.invoke (webpack:///D:/dev/nighthawk/frontend/~/zone.js/dist/zone.js?:392:26)
at ProxyZoneSpec.onInvoke (webpack:///D:/dev/nighthawk/frontend/~/zone.js/dist/proxy.js?:79:39)

在每个之前测试:

beforeEach(() => {
fixture = TestBed.createComponent(DashboardVideosListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

测试:

describe('set camera', () => {
it('success', async(() => {
component.camera(new CameraModel({
cameraId: 13
}));

expect(component._camera['cameraId']).toBe(13);
}));
});

最佳答案

代替

   component.camera(new CameraModel({
cameraId: 13
}));

你想要

component.camera = new CameraModel({});

因为它是一个输入 setter ,所以您并没有真正调用该函数,而是每次您给它一个新值时该函数都会运行。 (:

关于angular - 测试方法 setter @Input() Angular 4 Karma,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48681990/

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