笑话单元测试在组件创建中失败

时间:2019-07-08 20:38:35

标签: angular unit-testing jestjs

我的单元测试由于开玩笑而失败。大多数情况下,它们未通过组件创建测试。我在Jasmine单元测试中也进行了类似的测试,它们都成功了。

这是一个套件的代码:

    class MockAuthenticationService {
      canAccessFeature(featureId: number) {
      return true;
     }
    }

interface TestSuite {
  fixture: ComponentFixture<TestComponent>;
  testComponent: any;
  myComponentInstance: any;
  myComponentDom: any;
  mockConfigDetailsInstance: any;
  panelWrapperInstance: any;
}

describe('Component Details', () => {
    let suite: TestSuite;
    beforeAll(() => { suite = <TestSuite>{}; });
    afterAll(() => { suite = null; });

    beforeEach((done) => {
      TestBed.configureTestingModule({
        imports: [WhCoreLibModule.forRoot()],
        declarations: [
          TestComponent,
          ItemDetailsComponent,
          DetailsComponent,
          WrapperComponent
        ],
        providers: [
          { provide: WhAuthenticationService, useClass: MockAuthenticationService },
        ],
        schemas: [NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA]
      }).compileComponents().then(() => {
        suite.fixture = TestBed.createComponent(TestComponent);
        suite.testComponent = suite.fixture.componentInstance;

        let myDebugElement = suite.fixture.debugElement.children[0];
        if (myDebugElement) {
          suite.myComponentInstance = myDebugElement.componentInstance;
          suite.myComponentDom = myDebugElement.nativeElement;
          let panelDebugEl = myDebugElement.query(By.directive(WrapperComponent));
          let configDebugEl = myDebugElement.query(By.directive(DetailsComponent));

          if (configDebugEl) {
            suite.mockDetailsInstance = configDebugEl.componentInstance;
          }
          if (panelDebugEl) {
            suite.panelWrapperInstance = panelDebugEl.componentInstance;
          }
        }

        suite.fixture.detectChanges();
        done();
      }).catch((err: string) => done.fail(err));
    });

    it('should create component instance', () => {
      expect(suite.myComponentInstance).toBeTruthy(); // Fails here
      expect(suite.mockDetailsInstance).toBeTruthy();
      expect(suite.panelWrapperInstance).toBeTruthy();
    });

    it('should pass collapsed param to WrapperComponent', () => {
      suite.testComponent.configCollapsed = true;
      suite.fixture.detectChanges();
      expect(suite.panelWrapperInstance.expanded).toBeFalsy();
      suite.testComponent.configCollapsed = false;
      suite.fixture.detectChanges();
      expect(suite.panelWrapperInstance.expanded).toBeTruthy();
    });

    it('should forward collapsedChange event from WrapperComponent', () => {
      spyOn(suite.testComponent, 'onConfigCollapsedChange');
      suite.panelWrapperInstance.onExpandStateChanged.emit(true);
      expect(suite.testComponent.onConfigCollapsedChange).toHaveBeenCalledWith(false);
      suite.panelWrapperInstance.onExpandStateChanged.emit(false);
      expect(suite.testComponent.onConfigCollapsedChange).toHaveBeenCalledWith(true);
    });

    it('should emit editItem event from WrapperComponent actionClicked Event', () => {
      spyOn(suite.testComponent, 'onEditItem');
      suite.panelWrapperInstance.onActionClicked.emit();
      expect(suite.testComponent.onEditItem).toHaveBeenCalled();
    });
  });

@Component({
  template: '<item-details [detailsInput]="details" ' +
    '[configCollapsed]="configCollapsed" (configCollapsedChange)="onConfigCollapsedChange($event)" ' +
    '(editItem)="onEditItem($event)"></item-details>'
})
export class TestComponent {
  details = <ItemDetailsInput>{
    basedOnConfigId: 0,
    // optionsData: [{ IsPaint: false, CategoryDesc: '' }]
  };
  configCollapsed: boolean;

  onConfigCollapsedChange(collapsed: boolean) {/**/}
  onEditItem() {/**/}
}

@Component({
  selector: 'item-configuration-details',
  template: '<div></div>'
})
class DetailsComponent {
  @Input() itemConfigurationDetailsParams: any;

在升级到我们完成的angular 7之后,我实际上对单元测试还很陌生,我需要确保单元测试能够继续工作。

玩笑的版本与升级之前相同:

"jest": "^23.6.0",
"jest-junit": "^6.4.0",
"jest-preset-angular": "^6.0.2",
"babel-jest": "^24.7.1",

我已经在互联网上搜索了,但是很茫然。有人有想法么? 谢谢

0 个答案:

没有答案