为什么我的角度测试会给出不一致的结果?

时间:2019-02-14 17:46:47

标签: angular karma-jasmine ngrx

在我的角度应用程序中,我们有一个“ LayoutContainer”类,它仅显示我们网站的标题,然后是一个带有静态链接的导航栏。

我需要能够根据登录用户的身份来使链接动态化-如果他们是管理员,则希望能够显示一个新链接。我所做的是我从“帐户”模块中导入了减速器。

现在,我已经发现与单元测试不一致,并且当我对测试进行随机排序时,某些旧测试正在中断。其他时间,所有68个测试都通过。

以下是某些“应创建”类的组件偶尔弹出的错误:

Uncaught TypeError: Cannot read property 'authentication' of undefined
    at :9876/_karma_webpack_/webpack:/src/app/account/reducers/index.ts:42
    at :9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:528
    at memoized (:9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:484)
    at defaultStateFn (:9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:502)
    at :9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:531
    at memoized (:9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:484)
    at :9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:501
    at Array.map (<anonymous>)
    at defaultStateFn (:9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:501)
    at :9876/_karma_webpack_/webpack:/node_modules/@ngrx/store/fesm5/store.js:531
    at 

account / reducers / index.ts的第42行是:

// Reducers
import * as fromRoot from '../../reducers';
import * as fromAuthentication from './authentication.reducer';
import * as fromFacility from './club.reducer';
import * as fromRegistration from './registration.reducer';
import * as fromSubscription from './subscription.reducer';
import * as fromUserSubscription from './user-subscription.reducer';

import { RegistrationErrorTypes } from '../actions/registration.actions';
import { LEAVE_SELECTOR } from '../../../../node_modules/@angular/animations/browser/src/util';

export interface AccountState {
  authentication: fromAuthentication.State;
  facility: fromFacility.State;
  registration: fromRegistration.State;
  subscription: fromSubscription.State;
  userSubscription: fromUserSubscription.State;
}

export const 
selectAuthStatusState = createSelector(
  selectAccountState,
  (state: AccountState) => state.authentication  /// LINE 42 that is 'undefined'
);

疯狂的是,当我重新运行几次时,通常所有测试都通过了。问题可能是什么?

编辑-测试失败的示例

// Angular Core
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

// Angular Material
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';

// ngrx
import { StoreModule, combineReducers } from '@ngrx/store';

// Reducers
import * as fromAccount from '../../../account/reducers';

// Component
import { CancelSubscriptionDialogComponent } from './cancel-subscription-dialog.component';

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        StoreModule.forRoot({
          account: combineReducers(fromAccount.reducers)
        })
      ],
      providers: [
        { provide: MatDialogRef, useValue: {} },
        {
          provide: MAT_DIALOG_DATA,
          useValue: {}
        }
      ],
      declarations: [CancelSubscriptionDialogComponent]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(CancelSubscriptionDialogComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

0 个答案:

没有答案