角度测试错误:无法读取未定义的属性“管道”

时间:2020-05-20 12:12:51

标签: angular

我遇到以下测试错误:

✖应该创建 Chrome 81.0.4044(Mac OS X 10.14.6) TypeError:无法读取未定义的属性“管道” 在AccountsInfoComponent.loadCompanyTypes(webpack:///./src/app/contacts/contact/overview/accounts-info/accounts-info.component.ts?:75:13)中 在AccountsInfoComponent.initAccountsInfoForm(webpack:///./src/app/contacts/contact/overview/accounts-info/accounts-info.component.ts?:129:14)中 在AccountsInfoComponent.updateContactAccounts(webpack:///./src/app/contacts/contact/overview/accounts-info/accounts-info.component.ts?:89:22)中 在AccountsInfoComponent.set [作为联系人]中(webpack:///./src/app/contacts/contact/overview/accounts-info/accounts-info.component.ts?:47:18) 在UserContext.eval(webpack:///./src/app/contacts/contact/overview/accounts-info/accounts-info.component.spec.ts?:120:27)

我通过这种方法提供服务

getCompanyType(contact_id: string) {
    const url = `api/v1/contact/sponsors_list?contact_id=${contact_id}`
    return this.http.get<any>(url)
  }

以及使用此方法的组件:

loadCompanyTypes() {
    this.editService
      .getCompanyType(this.contact.id)
      .pipe(untilDestroyed(this))
      .subscribe(classic_where_held => {
        const arrayOfTypes = classic_where_held.where_held_classic_id
        this.companyTypes = arrayOfTypes.map(x => {
          return { classic_id: x[0], name: x[1] }
        })
      })
  }

我在测试中嘲笑了我的服务方法:

const mockContactEditService = {
    addEditSection: jasmine.createSpy('addEditSection'),
    setSaveUpdateContact: jasmine.createSpy('addEditSection'),
    getCompanyType: jasmine.createSpy('getCompanyType')
  }

测试在这里.pipe(untilDestroyed(this))中断,它说getCompanyType不返回任何内容,它应该返回一个Observable ??

1 个答案:

答案 0 :(得分:1)

我发现了问题,我需要以这种方式更改我的模拟:

getCompanyType: jasmine.createSpy('getCompanyType').and.returnValue(of(mockCompanyTypes))

这解决了问题!