Jasmin Unit-test - Nicht Lesen-Eigenschaft pipe undefined

Ich bin mit Winkel-6, NgRx 6, RxJS 6.

Habe ich eine route Wächter, der sah aus wie dieser -

import { CanActivate, ActivatedRouteSnapshot } from '@angular/router';
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { IAppState } from '../../../app.state';
import { Store } from '@ngrx/store';

import { SetTenant } from './../../../store/config/config.actions';

@Injectable()
export default class TenantGuard implements CanActivate {
  constructor(private store: Store<IAppState>) {}

  canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
    const tenant = route.params['tenant'];

    if (!tenant) {
      return of(false);
    }

    this.store.dispatch(new SetTenant(tenant));
    return of(true);
  }
}

Wie Sie sehen können, ich war das hinzufügen meiner tenant den store über this.store.dispatch(new SetTenant(tenant));

War dies jedoch verursacht, dass die Maßnahmen zur Feuer-jedes mal, wenn ein Benutzer besucht die base route.

Um dieses zu bekämpfen, habe ich Hinzugefügt, um zu sehen, wenn die tenant gefüllt ist und nur Feuer, die Aktion, wenn nicht -

import { CanActivate, ActivatedRouteSnapshot } from '@angular/router';
import { Injectable } from '@angular/core';
import { Observable, of, combineLatest } from 'rxjs';
import { IAppState } from '../../../app.state';
import { Store, select } from '@ngrx/store';

import { SetTenant } from './../../../store/config/config.actions';
import { getTenant } from '../../../store/config/config.selectors';
import { map } from 'rxjs/operators';

@Injectable()
export default class TenantGuard implements CanActivate {
  constructor(private store: Store<IAppState>) {}

  canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
    const tenantFromRoute: string = route.params['tenant'];

    return this.store.pipe(select(getTenant)).pipe(
      map(tenantFromStore => {
        if (!tenantFromRoute) {
          return false;
        }

        if (!tenantFromStore) {
          this.store.dispatch(new SetTenant(tenantFromRoute));
        }
        return true;
      })
    );
  }
}

Diese hat jedoch gebrochen meine unit-tests, da habe ich eingeführt, zusätzliche Logik und ich bin jetzt immer der Fehler TypeError: Cannot read property 'pipe' of undefined

Meine spec-Datei sieht wie folgt aus -

import { TestBed, async } from '@angular/core/testing';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { Store } from '@ngrx/store';
import { StoreModule } from '@ngrx/store';

import { SetTenant } from './../../../store/config/config.actions';

import TenantGuard from './tenant.guard';

describe('TenantGuard', () => {
  it('should return false if a tenant is not present on the route', async(() => {
    const { tenantGuard, props } = setup({});
    let result: boolean;
    tenantGuard.canActivate(props).subscribe(canActivate => (result = canActivate));
    expect(result).toBeFalsy();
  }));

  it('should return true if a tenant is present on the route', async(() => {
    const { tenantGuard, props } = setup({ tenant: 'main' });
    let result: boolean;
    tenantGuard.canActivate(props).subscribe(canActivate => (result = canActivate));
    expect(result).toBeTruthy();
  }));

  it('should dispatch an action to set the tenant in the store', () => {
    const { store, tenantGuard, props } = setup({ tenant: 'foo' });
    const action = new SetTenant('foo');
    tenantGuard.canActivate(props);

    expect(store.dispatch).toHaveBeenCalledWith(action);
  });

  it('should not dispatch an action to set the tenant in the store if the tenant is missing', () => {
    const { store, tenantGuard, props } = setup({});
    tenantGuard.canActivate(props);

    expect(store.dispatch).not.toHaveBeenCalled();
  });

  const setup = propOverrides => {
    TestBed.configureTestingModule({
      imports: [StoreModule.forRoot({})],
      providers: [
        TenantGuard,
        {
          provide: Store,
          useValue: jasmine.createSpyObj('Store', ['dispatch', 'pipe']),
        },
      ],
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
    }).compileComponents();

    const props = Object.assign({ params: { tenant: null } }, { params: { ...propOverrides } });

    const tenantGuard = TestBed.get(TenantGuard);
    const store = TestBed.get(Store);

    return { tenantGuard, props, store };
  };
});

Habe ich Hinzugefügt pipe zu meinem jasmine.createSpyObj aber ich bin nicht sicher, wie Sie Fortschritte.

Möchte ich schreiben, weitere tests, um dieses, aber ich habe Probleme Spott heraus, wie pipe sollte /würde in diesem Fall verwendet werden.

Bearbeiten - Wenn ich nicht pipe in meinem jasmine.createSpyObj ich stattdessen die Fehlermeldung ​​TypeError: this.store.pipe is not a function​​

  • Also ich denke die Frage könnte sein, wie ich bin mocking meinem Shop.
  • Habe diesen Fehler habe gelöst..??
InformationsquelleAutor Harry Blue | 2018-06-13
Schreibe einen Kommentar