【问题标题】:Resolved: Mocking Angular Dialog afterClosed() for unit test已解决:Mocking Angular Dialog afterClosed() 进行单元测试
【发布时间】:2021-01-07 12:15:37
【问题描述】:

我在为单元测试模拟弹出数据时遇到了麻烦。收到如下错误:TypeError: Cannot read property 'showFilterModal' of undefined 我做错了什么?

过滤器组件

public openFilterModal() {
  this.filterModal = this.modalService.showFilterModal(this.filtersOptions, this.userRole, 'bills');
  this.filterModal.afterClosed
    .subscribe(
      (result) => {
        this.selectedFiltersAmount = result.total;
        this.filtersOptions = result.filters;
      },
      (err) => {

      });
}

这个想法是创建具有必要的可观察对象和功能的假服务。非常感谢@Naren

正确的测试模拟:

describe('Component', () => {
  let component: Component;
  let fixture: ComponentFixture<Component>;

  const mockModelService = {
    showFilterModal: 
    jasmine.createSpy('showFilterModal').and.returnValue({ 
      afterClosed: of({
        total: 1,
        filters: null,
        reqObj: {
          status: 'active',
        }
      })})
    };

   beforeEach(() => {
    TestBed.configureTestingModule({
     imports: [NecessaryModules],
     providers: [
      { provide: LocalModalService, useValue: mockModelService }
     ],
     declarations: [ Component]
    }).compileComponents();
    fixture = TestBed.createComponent(Component);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create subscription after close modal', () => {
    component.openFilterModal();
    expect(mockModelService.showFilterModal).toHaveBeenCalled();
    expect(component.selectedFiltersAmount).toBe(1);
  });
});

【问题讨论】:

    标签: angular testing


    【解决方案1】:

    您可以在创建组件时模拟modelService 并放入providers,并监视service methods 并定义预期值。

    您可以尝试这样,但请确保您已根据您的组件更新了必要的更改。如果有任何错误,请告诉我。

    import { of } from 'rxjs';
    import YourModelService from '/path/serives'
    
    
    const mockModelService = {
      showFilterModal: jasmine.createSpy('showFilterModal').and.returnValue({ afterClosed: of({
      total: 1,
      filters: null,
      reqObj: {
        status: 'active',
      }
    } })
    }
    
    describe('testing', () => {
      beforeEach(() => {
        TestBed.configureTestingModule({
          declarations: [YourComponent],
          providers: [
            {
              provide: YourModelService,
              useValue: mockModelService // <-- here mocked the service
            }
          ],
          schemas:[CUSTOM_ELEMENTS_SCHEMA]
        });
      })
      
      it('should create subscription after close modal', () => {
        component.openFilterModal();
      
        expect(mockModelService.showFilterModal).toHaveBeenCalled();
        expect(afterClosedSpy).toHaveBeenCalled();
        expect(component.selectedFiltersAmount).toBe(1);
      });
    })
    
    

    【讨论】:

    • 遇到错误:TypeError: this.filterModal.afterClosed.subscribe is not a function
    • 你能再检查一次,更新一点
    • 同样的问题。我放置了正确的提供者,但没有帮助。我还将“YourModelService”公开为主要组件,但没有帮助。
    • 您需要向我们展示您在做什么,我们不能假设您的文件中有什么。提供所有规范代码,然后会看到
    • 我已经稍微更改了您的代码,就像那样并且它可以工作,并且我已经删除了 afterClosedSpy,只是检查更改的值: const mockModelService = { showFilterModal: jasmine.createSpy('showFilterModal').and。 returnValue({ afterClosed: of({ total: 1, filters: null, reqObj: { status: 'active', } })}) };非常感谢!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2018-03-07
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2013-05-26
    相关资源
    最近更新 更多