angular - 角度测试:使用特定类型的参数测试方法
问题描述
我是测试新手,我遇到了以下情况。我的组件有一个closeBox()
采用 type 参数的方法Conversation
。Conversation
是一个非常复杂的模型(见下文)。如果我尝试通过使用简单对象 {id:1, name: 'me'} 调用它来测试此方法,它会要求它必须是 type 的参数Conversation
。
现在,我想知道:每次我想用特定类型的参数测试方法时,我真的必须伪造整个对话对象吗?这种对话模型已经很麻烦了,而且还没有那么大。当然有更好的方法来做到这一点。任何人?
这是我要执行的测试:我确保数组属性中存在虚假对话openConversations
,然后我调用 closeBox 方法,并将对话作为参数。如您所见,创建这种虚假对话需要做很多工作。有一个更好的方法吗?
it('should close the chatbox', function () {
const mockConversation = new Conversation(
[{id: 'jos'}],
{username: 'jos', profilePicture: {name: 'jos', uploaded: true, userId: '12345'}},
'123456',
'123457',
'9875412');
component.openConversations = [mockConversation];
fixture.detectChanges();
component.closeBox(mockConversation);
expect(component.openConversations).toBe(null);
});
组件方法:this.openConversations 是该组件的一个属性。这是一系列对话
closeBox(openConv: Conversation) {
const index = this.openConversations.indexOf(openConv);
if (index > -1) {
this.openConversations.splice(index, 1);
}
}
对话模式
导出类对话{
constructor(
public messages: Array<{}>,
public otherUser: {
username: string,
profilePicture: {
name: string,
uploaded: boolean,
userId: string
}},
public user1: string,
public user2: string,
public _id: string
) {}
}
解决方案
推荐阅读
- regex - 如何调试传递给 grep 的(PCRE)正则表达式?
- java - Android Studio No view found for id Error
- javascript - 如何更改 Chart.js 图表中文本的颜色
- snowflake-cloud-data-platform - 如何从雪花的数据库表中将超过 100MB 的数据下载到 csv 中
- python - 根据另一列的值在 Pandas 中创建新列
- python - 根据列中的重复值范围从 pandas df 获取数据块
- wix - 仅 32/64 位 WiX 组合安装程序 - 复制文件
- c++ - 如何在 C++ 中使用光标定位
- java-8 - 在 java 8 时间与 joda 时间中设置时区偏移
- python - 将 Python 更新到 3.8 并且模块不起作用