角度测试:使用类型特定的参数测试方法

时间:2018-09-19 23:56:07

标签: angular unit-testing testing jasmine

我不熟悉测试,因此遇到了以下情况。我的组件有一个方法closeBox(),它使用类型为Conversation的参数。 Conversation是一个非常复杂的模型(请参见下文)。如果我尝试通过使用简单对象{id:1,name:'me'}调用此方法来测试此方法,则它要求它必须是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
  ) {}

}

0 个答案:

没有答案
相关问题