茉莉花,角 - 如何测试特性主张?
茉莉花 - Angular
这是正在测试的组件的一部分(测试ThisingThisComppnent):
import { ThatComponent } from '.../shared/components';
export class TestingThisComppnent {
(...)
@ViewChild('child', { static: true })
public map: GoogleMapComponent;
public ngOnInit(): void {
(...)
this.map.infoWindowComponent = ThatComponent;
}
}
GoogleMapComponent:
export class GoogleMapComponent implements OnInit, OnDestroy, OnChanges {
(...)
@Input()
public infoWindowComponent: any;
(...)
}
如何测试这一行? -
this.map.infoWindowComponent = WorkItemInfoWindowComponent;
我们不是在项目中使用componentFixture,而是通过“ new”创建组件类 -
subject = new WorkItemLocationTabComponent(
firstSpy,
secondSpy,
thirdSpy,
fourthSpy
);
我尝试了创建间谍 -
infoWindowComponentSpy = spyOnProperty(subject.map, 'infoWindowComponent');
但是(我猜)“ InfowindowComponent”是一个@input(),我正在得到一个错误,属性“没有访问类型get”,因此我添加了get()和set() -
Object.defineProperty(subject.map, 'infoWindowComponent', {
get: function () {
return this.value;
},
set: function (val) {
this.value = val;
return this.value;
},
});
但是,由于abfiets.map.infowdowcomponent的登录,测试仍然不起作用,始终返回“未定义” 。
Jasmine - Angular
This is a part of the component, that is being tested (TestingThisComppnent ):
import { ThatComponent } from '.../shared/components';
export class TestingThisComppnent {
(...)
@ViewChild('child', { static: true })
public map: GoogleMapComponent;
public ngOnInit(): void {
(...)
this.map.infoWindowComponent = ThatComponent;
}
}
GoogleMapComponent:
export class GoogleMapComponent implements OnInit, OnDestroy, OnChanges {
(...)
@Input()
public infoWindowComponent: any;
(...)
}
How can I test this line? -
this.map.infoWindowComponent = WorkItemInfoWindowComponent;
We are not using ComponentFixture in the project, but creating a component class by 'new' -
subject = new WorkItemLocationTabComponent(
firstSpy,
secondSpy,
thirdSpy,
fourthSpy
);
I've tried by creating a Spy -
infoWindowComponentSpy = spyOnProperty(subject.map, 'infoWindowComponent');
but because (I guess) 'infoWindowComponent' is an @Input(), I'm getting an error, that the property 'does not have access type get', so I've added get() and set() -
Object.defineProperty(subject.map, 'infoWindowComponent', {
get: function () {
return this.value;
},
set: function (val) {
this.value = val;
return this.value;
},
});
But still, the test doesn't work, because log of subject.map.infoWindowComponent always returns 'undefined'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您不使用
testbed
,并且仅执行ViewChild
将永远无法填充MAP
变量。尝试这样做:
Since you're not using
TestBed
and just doingViewChild
will never be able to populate themap
variable.Try doing this: