我正试图在HttpInterceptor的帮助下测试ng-mocks
@Injectable()
export class AuthTokenInterceptor implements HttpInterceptor {
constructor(private store: Store) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const currentToken = this.store.selectSnapshot(state => state.accounts.accessToken);
if (currentToken) {
req = req.clone({
setHeaders: { Authorization: `Bearer ${currentToken}` },
});
}
return next.handle(req);
}
}我很难了解如何提供next参数,特别是因为我想检查对象是否存在setHeaders属性(或不存在):
describe('Unauth guard test', () => {
beforeEach(() => {
return MockBuilder(AuthTokenInterceptor, AppModule).mock(Store);
});
it('should allow access if not authenticated', () => {
//Arrange
const fakeToken = 'fakeToken';
const storeDispatchSpy = MockInstance(Store, 'selectSnapshot', jasmine.createSpy().and.returnValue(fakeToken));
const httpHandlerSpy = MockInstance(HttpHandler, 'handle', jasmine.createSpy().and.returnValue(fakeToken));
const fixture = MockRender(AuthTokenInterceptor);
const interceptor = fixture.point.componentInstance;
const request = new HttpRequest('GET', 'http://localhost:4200/');
//Act
const result = interceptor.intercept(request, httpHandlerSpy.???);//How to provide a mock of HttpHandler?
//Assert
expect(storeDispatchSpy).toHaveBeenCalled();
expect(httpHandlerSpy).toHaveBeenCalledOnceWith(???);//How to verify that one parameter of the HttpRequest is set to specific value?
});
});但是如何向拦截方法提供模拟实例呢?更复杂的是,如何检查我的间谍是否被一个具有特定价值的对象调用?
发布于 2022-05-09 17:23:31
在您的情况下,您需要提供mockStore +将拦截器与其他拦截器隔离开来。
一个来自ng-mocks docs and http interceptors的例子。
https://codesandbox.io/s/intelligent-stallman-9bwup0?file=/src/test.spec.ts
describe('AuthTokenInterceptor', () => {
beforeEach(() => {
return (
MockBuilder(AuthTokenInterceptor, AppModule)
// required for interceptors
.exclude(NG_MOCKS_INTERCEPTORS)
.keep(HTTP_INTERCEPTORS)
.replace(HttpClientModule, HttpClientTestingModule)
);
});
it('adds header', () => {
// creating an empty fixture
const fixture = MockRender('', null, false);
// stubbing store
const store = ngMocks.findInstance(Store);
ngMocks.stubMember(store, 'selectSnapshot', callback =>
callback({
accounts: {
accessToken: 'Testing',
},
}),
);
// render
fixture.detectChanges();
// instances to test how HttpClient uses interceptors
const client = ngMocks.findInstance(HttpClient);
const httpMock = ngMocks.findInstance(HttpTestingController);
// Let's do a simple request.
client.get('/target').subscribe();
// Now we can assert that a header has been added to the request.
const req = httpMock.expectOne('/target');
req.flush('');
httpMock.verify();
// asserting headers
expect(req.request.headers.get('Authorization')).toEqual(
'Bearer Testing',
);
});
});https://stackoverflow.com/questions/72174210
复制相似问题