我使用的是Angular 6,NgRx 6,RxJS 6.
我有一个看起来像这样的路线保护 -
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);
}
}
如您所见,我通过tenant
this.store.dispatch(new SetTenant(tenant));
添加到商店
然而,每当用户访问基本路线时,就会触发该动作。
为了解决这个问题,我添加了一项检查,看看是否填充了tenant
,如果没有,则只触发操作 -
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;
})
);
}
}
然而,这已经破坏了我的单元测试,因为我已经引入了额外的逻辑,现在我收到了错误TypeError: Cannot read property 'pipe' of undefined
我的spec文件看起来像这样 -
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 };
};
});
我已将pipe
添加到jasmine.createSpyObj
,但我不确定如何进展。
我想围绕这个进行额外的测试,但是我很难嘲笑在这种情况下{/ 1}}应该/将如何使用。
修改 - 如果我没有将pipe
传递给我的pipe
,我会收到错误jasmine.createSpyObj
答案 0 :(得分:0)
我有和您一样的错误信息。我将路由器注入组件中,并使用了this.router.events.pipe(...)...在测试中,我对路由器使用了存根。在routerStub看起来像这样之前:
routerStub = {
navigate: (commands: any[]) => { Promise.resolve(true); },
};
因此您可以看到,在我需要在组件中使用路由器的Navigation方法之前,我在存根中对其进行了定义。现在,我还需要events属性,该属性返回一个可观察的位置,而不是使用.pipe的位置。我将以下内容添加到为我修复的routerStub中:
routerStub = {
navigate: (commands: any[]) => { Promise.resolve(true); },
events: of(new Scroll(new NavigationEnd(0, 'dummyUrl', 'dummyUrl'), [0, 0], 'dummyString'))
};
在我的情况下,我需要一个Scroll Event才能使代码正常工作,但是现在事件在我的存根中定义为Observable,并且现在知道管道。
也许这可以帮助您...