简体   繁体   English

Angular 单元测试:属性订阅没有访问类型获取

[英]Angular unit test: Property subscribe does not have access type get

Trying to run unit test of observable property when value comes with type error.当值带有类型错误时,尝试运行可观察属性的单元测试。

Running the test I get: Error: Property subscribe does not have access type get运行我得到的测试:错误:属性订阅没有访问类型获取

What am I doing wrong?我究竟做错了什么? Thank you for the help.感谢您的帮助。

app-list.component.ts应用程序列表.component.ts

export class TransferComponent {
    transfers$: Observable<Transfer[] | AppResponseError>;
    constructor(
    private transfersService: TransfersService) {
    this.transfers$ = this.transfersService.transfers$;
  }
}

app-list.component.spec.ts应用程序列表.component.spec.ts

it('should hide app-checklist component when return AppResponseError', () => {
    const error : AppResponseError = {
      code:'400',
      message:'Bad request'
    };    
    spyOnProperty(component.transfers$, 'subscribe').and.returnValue(of(error));
    fixture.detectChanges(); 
    expect(fixture.debugElement.query(By.css('app-checklist'))).toBeNull();
  });

From issue :问题

spyOnProperty is intended for use on objects that have used Object.defineProperty since that is implemented with a function behind it and can't be modified by just assigning to it. spyOnProperty旨在用于已使用Object.defineProperty的对象,因为它是通过 function 实现的,并且不能通过仅分配给它来修改。 If you just have basic properties on an object, you should be able to just assign them.如果您只有 object 的基本属性,您应该可以分配它们。

You can just assign a stub value to component.transfers$ property.您可以只为component.transfers$属性分配一个存根值。

Eg例如

component.ts : component.ts

import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { TransfersService } from './TransfersService';

export type AppResponseError = any;
type Transfer = any;

@Component({
  selector: 'app-transfer',
})
export class TransferComponent implements OnInit {
  transfers$: Observable<Transfer[] | AppResponseError>;
  constructor(private transfersService: TransfersService) {
    this.transfers$ = this.transfersService.transfers$;
  }

  ngOnInit() {
    this.transfers$.subscribe((data) => {
      console.log(data);
    });
  }
}

TransferService.ts : TransferService.ts

import { Injectable } from '@angular/core';
import { of } from 'rxjs';

@Injectable()
export class TransfersService {
  transfers$ = of('real observable');
}

component.test.ts : component.test.ts

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { AppResponseError, TransferComponent } from './component';
import { TransfersService } from './TransfersService';

fdescribe('65208911', () => {
  let component: TransferComponent;
  let fixture: ComponentFixture<TransferComponent>;
  const transfersServiceStub = {
    transfers$: of(),
  };
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [TransferComponent],
      providers: [
        { provide: TransfersService, useValue: transfersServiceStub },
      ],
    });
    fixture = TestBed.createComponent(TransferComponent);
    component = fixture.componentInstance;
  });
  it('should hide app-checklist component when return AppResponseError', () => {
    const error: AppResponseError = {
      code: '400',
      message: 'Bad request',
    };
    component.transfers$ = of(error);
    fixture.detectChanges();
    // expect(fixture.debugElement.query(By.css('app-checklist'))).toBeNull();
  });
});

unit test result:单元测试结果:

LOG: Object{code: '400', message: 'Bad request'}
Chrome 80.0.3987.87 (Mac OS 10.13.6): Executed 0 of 15 SUCCESS (0 secs / 0 secs)
WARN: 'Spec '65208911 should hide app-checklist component when return AppResponseError' has no expectations.'
Chrome 80.0.3987.87 (Mac OS 10.13.6): Executed 0 of 15 SUCCESS (0 secs / 0 secs)
Chrome 80.0.3987.87 (Mac OS 10.13.6): Executed 1 of 15 (skipped 14) SUCCESS (0.086 secs / 0.026 secs)
TOTAL: 1 SUCCESS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我在服务中添加了订阅并对该服务进行了单元测试,我将获得“订阅”类型上不存在属性“订阅”吗? - If i add subscribe in service and unit test of that service, i'm getting Property 'subscribe' does not exist on type 'Subscription'? Angular jasmine:属性没有访问类型获取 - Angular jasmine: Property does not have access type get Angular 9 单元测试属性 'of' 不存在于类型 'typeof Observable' - Angular 9 unit test Property 'of' does not exist on type 'typeof Observable' Karma:属性没有访问类型 get - Karma: property does not have access type get 类型 void 上不存在 Angular 属性订阅 - Angular Property subscribe does not exist on type void 角度 9 中的“void”类型不存在属性“subscribe” - Property 'subscribe' does not exist on type 'void' in angular 9 角度2中的类型&#39;void&#39;上不存在属性&#39;subscribe&#39; - Property 'subscribe' does not exist on type 'void' in angular 2 Angular 9 - 类型“void”上不存在属性“subscribe” - Angular 9 - Property 'subscribe' does not exist on type 'void' 单元测试中的Angular“无法读取未定义的属性&#39;订阅&#39;” - Angular “Cannot read property 'subscribe' of undefined” in Unit Test TypeError:无法读取未定义的属性“ subscribe”(角度单元测试) - TypeError: Cannot read property 'subscribe' of undefined (Angular Unit-Test)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM