简体   繁体   English

如何使用茉莉花大理石在Observable订阅中测试代码

[英]How to test code inside Observable subscription with jasmine marbles

I have a component that subscribes to observable returned from service in ngOnInit. 我有一个组件,它订阅了ngOnInit服务中返回的observable。 I also have method (updateData) which subscribes to observable from service but also updates some property inside subscription. 我也有方法(updateData),该方法从服务订阅可观察的内容,但也更新订阅中的某些属性。 I need to create a test to check if this property is changed to correct value when updateData is called. 我需要创建一个测试来检查在调用updateData时此属性是否更改为正确的值。

Component snippet: 组件片段:

ngOnInit() {
    this.myService.loadData().subscribe() => {
        // some code here
    });
}

...

public updateData() {
    this.myService.updateData(this.data).subscribe(() => {
        this.dataUpdated = true;
    });
}

Test setup 测试设置

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<MyComponent>;
  let myServiceSpy: jasmine.SpyObj<MyService>;

  const dataMock = {};
  const updateResponseMock = {};

  beforeEach(async(() => {
    myServiceSpy = jasmine.createSpyObj('DesignPickerService', [
      'loadData',
      'updateData'
    ]);
    myServiceSpy.loadData.and.returnValue(
      cold('-a|', { a: dataMock })
    );
    myServiceSpy.updateData.and.returnValue(
      cold('--b|', { b: updateResponseMock })
    );
    TestBed.configureTestingModule({
      declarations: [
        MyComponent,
      ],
      providers: [
        {
          provide: MyService,
          useFactory: () => {
            return myServiceSpy;
          }
        }
      ]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });
  describe('Tests', () => {
    it('updateData should set dataUpdated to true', () => {
      ??? // some code to wait for myService.loadData to finish
      component.dataUpdated = false;
      component.updateData();
      ??? // some code to wait for myService.updateData to finish
      expect(component.dataUpdated).toBe(true);
    })
  });
});

I want to add those missing lines so the test would pass. 我想添加那些缺少的行,以便测试通过。 Any help would be appreciated. 任何帮助,将不胜感激。

I'm sorry i tought you're talking about an http call, you can try this way but i didn't use service, you can easily mock it. 对不起,我要告诉您一个http呼叫,您可以尝试这种方式,但是我没有使用service,您可以轻松地对其进行嘲笑。

fit('should return correct data', (done) => { fit('应该返回正确的数据',(完成)=> {

expect(component.dataUpdated).toEqual(false);

component.getData().subscribe(data => {
  expect(data).toEqual([1, 2, 3, 4]);
  expect(component.dataUpdated).toEqual(true);
  done();
}
  );

  });


export class ObservableTddComponent implements OnInit {

  dataUpdated;

  constructor() {
    this.dataUpdated = false;
  }

  ngOnInit() {
    this.getData().subscribe( (x) => {
      this.dataUpdated = true;
    });
  }

  getData(){
    return of([1,2,3,4]).pipe(delay(500));
  }

}

You can find more about it, reading this article: https://itnext.io/a-quick-tip-on-testing-observables-e2fbdebef4c 您可以阅读本文,找到更多关于它的信息: https : //itnext.io/a-quick-tip-on-testing-observables-e2fbdebef4c

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM