首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >窥探与茉莉花的可观订阅的结果

窥探与茉莉花的可观订阅的结果
EN

Stack Overflow用户
提问于 2017-03-31 08:05:27
回答 4查看 15.1K关注 0票数 1

我是茉莉花单元,测试一个角度部件,它使用可观测值。我的组件有一个我正在测试的生命周期挂钩:

代码语言:javascript
复制
ngOnInit() {
  this.dataService.getCellOEE(this.cell).subscribe(value => this.updateChart(value));
}

我有一个测试可以确保getCellOEE已经被调用了,但是现在我想检查当可观察到的值用新值解析时是否调用了updateChart。到目前为止,这就是我所拥有的:

代码语言:javascript
复制
let fakeCellService = {
  getCellOEE: function (value): Observable<Array<IOee>> {
    return Observable.of([{ time: moment(), val: 67 }, { time: moment(), val: 78 }]);
  }
};

describe('Oee24Component', () => {
  let component: Oee24Component;
  let service: CellService;
  let injector: Injector;
  let fixture: ComponentFixture<Oee24Component>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [Oee24Component],
      providers: [{ provide: CellService, useValue: fakeCellService }]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(Oee24Component);
    component = fixture.componentInstance;
    injector = getTestBed();
    service = injector.get(CellService)
    fixture.detectChanges();
    spyOn(service, 'getCellOEE').and.returnValue({ subscribe: () => { } });
    spyOn(component, 'updateChart');
  });

  it('should get cell oee on init', () => {
    component.ngOnInit();
    expect(service.getCellOEE).toHaveBeenCalled();
  });

  it('should update chart on new data', () => {
    component.ngOnInit();
    expect(component.updateChart).toHaveBeenCalled();
  });
});

但是,我得到了错误:

chrome 56.0.2924 (Windows100.0.0) Oee24Component应该更新新数据的图表 预计间谍updateChart会被呼叫。

这大概是一个时间问题,因为当测试检查时,可观察到的不一定已经解决了?如果是这样的话,我该如何正确地设置它?

更新:

这是我的组成部分:

代码语言:javascript
复制
@Component({
  selector: 'app-oee24',
  templateUrl: './oee24.component.html',
  styleUrls: ['./oee24.component.css']
})
export class Oee24Component implements OnInit {
  public barChartData: any[] = [{ data: [], label: 'OEE' }];

  constructor(public dataService: CellService) { }

  ngOnInit() {
    this.dataService.getCellOEE(this.cell).subscribe(value => this.updateChart(value));
  }

  updateChart(data: Array<IOee>) {
    this.barChartData[0].data = data.map(val => val.val);
  }
}
EN

回答 4

Stack Overflow用户

发布于 2017-12-14 21:36:32

你想过解决办法吗?如何使用jasmine-marbles包和complete事件?

代码语言:javascript
复制
it('should update chart on new data', () => {
    const obs$ = cold('--a-|');
    spyOn(service, 'getCellOEE').and.returnValue(obs$);
    component.ngOnInit(); 
    obs$.subscribe({
        complete: () => {
            expect(component.updateChart).toHaveBeenCalledWith('a');
        }
    });
});
票数 0
EN

Stack Overflow用户

发布于 2018-07-04 20:13:55

不确定这是否是最好的方法,但我看到它在我正在做的一个项目上工作。这种方法基本上是获取对提供给订阅方法的回调函数的引用,并手动调用它以模拟发出值的观察者:

代码语言:javascript
复制
it('should update chart on new data', () => {
    component.ngOnInit();

    // this is your mocked observable
    const obsObject = service.getCellOEE.calls.mostRecent().returnValue;

    // expect(obsObject.subscribe).toHaveBeenCalled() should pass

    // get the subscribe callback function you provided in your component code
    const subscribeCb = obsObject.subscribe.calls.mostRecent().args[0];

    // now manually call that callback, you can provide an argument here to mock the "value" returned by the service
    subscribeCb(); 

    expect(component.updateChart).toHaveBeenCalled();
  });
票数 0
EN

Stack Overflow用户

发布于 2020-11-08 21:30:41

而不是

代码语言:javascript
复制
spyOn(service, 'getCellOEE').and.returnValue({ subscribe: () => { } });

你可以试试

代码语言:javascript
复制
spyOn(service, 'getCellOEE').and.returnValue( {subscribe: (callback) => callback()});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43135596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档