带有模拟存储的NGRX单元测试

时间:2019-07-10 14:28:45

标签: ngrx zonejs angular-unit-test

因此,我正在尝试为Angular + NGRX应用编写单元测试。我的服务需要使用Store,并且我按照NGRX的说明进行了模拟:

fdescribe('AuthGuardService', () => {
  let authGuardService: AuthGuardService;
  let store: MockStore<AppState>;

  const authState: AuthState = {
    authToken: 'somevalidtoken'
  };

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [],
      providers: [AuthGuardService, provideMockStore()]
    });
    store = TestBed.get(Store);
    authGuardService = TestBed.get(AuthGuardService);
    store.setState({auth: authState} as AppState);
  });

  it('should be created', () => {
    expect(authGuardService).toBeTruthy();
  });

});

我的服务如下:

@Injectable()
   export class AuthGuardService implements CanLoad, CanActivate {
   constructor(private store: Store<AppState>, private router: Router){}
...
}

我收到以下错误:

        Error: Zone.js has detected that ZoneAwarePromise `(window|global).Promise` has been overwritten.
    Most likely cause is that a Promise polyfill has been loaded after Zone.js (Polyfilling Promise api is not necessary when zone.js is loaded. If you must load one, do so before loading zone.js.)
        at Function../node_modules/zone.js/dist/zone.js.Zone.assertZonePatched (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:58:1)
        at new NgZone (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/core.js:24224:1)
        at TestBedViewEngine.push../node_modules/@angular/core/fesm5/testing.js.TestBedViewEngine._initIfNeeded (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/testing.js:2316:1)
        at TestBedViewEngine.push../node_modules/@angular/core/fesm5/testing.js.TestBedViewEngine.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/testing.js:2391:1)
        at Function.push../node_modules/@angular/core/fesm5/testing.js.TestBedViewEngine.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/testing.js:2179:1)
        at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/core/auth/auth-guard.service.spec.ts:21:21)
        at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:391:1)
        at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:308:1)
        at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:390:1)
        at Zone../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:150:1)

    Error: Expected undefined to be truthy.
        at <Jasmine>
        at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/core/auth/auth-guard.service.spec.ts:27:30)
        at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:391:1)
        at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:308:1)

所以从错误堆栈中看来,似乎发生了TestBed.get(Store),但有趣的是,当我更改了将它们更改为

的顺序时
authGuardService = TestBed.get(AuthGuardService);
store = TestBed.get(Store);

我在TestBed.get(AuthGuardService)上遇到相同的错误

0 个答案:

没有答案