我正在尝试测试处理SignalR连接的Angular服务,该服务将SignalR的代码作为InjectionToken。
这是提供程序文件:
// signalr-provider.ts
import { InjectionToken } from '@angular/core';
export const SIGNALR_TOKEN = new InjectionToken('signalR');
export function signalRFactory() {
return window['signalR'];
}
export const SIGNALR_PROVIDER = [ { provide: SIGNALR_TOKEN, useFactory: signalRFactory } ];
这是服务:
// signalr-service.ts
import { Injectable, Inject } from '@angular/core';
import { SIGNALR_TOKEN } from './signalr-provider';
import { HubConnection } from '@aspnet/signalr';
import { environment } from '../../../environments/environment';
@Injectable()
export class SignalrService {
private hubConnection: HubConnection;
private baseUrl: string = environment.baseUrl;
constructor(@Inject(SIGNALR_TOKEN) private signalR: any) {
this.init();
}
init(): void {
this.hubConnection = new this.signalR.HubConnectionBuilder().withUrl(`${this.baseUrl}/hubs/test`).build();
}
}
令牌在应用模块中的提供方式如下:
// From app.module.ts
@NgModule({
declarations: [...],
imports: [...],
providers: [ SIGNALR_PROVIDER, SignalrService ],
bootstrap: [ AppComponent]
})
export class AppModule {}
到目前为止,我没有运气尝试在测试文件中模拟InjectionToken,并每次都以NullInjectorError: No provider for InjectionToken signalR!
消息结束。
这是我的第一次尝试,从signalRFactory函数返回一个仅包含初始连接所需属性的模拟对象:
// signalr-service.spec.ts
import { TestBed, async } from '@angular/core/testing';
import { InjectionToken } from '@angular/core';
import { SignalrService } from './signalr-service';
const SIGNALR_TOKEN = new InjectionToken('signalR');
function signalRFactory() {
return {
HubConnectionBuilder: () => {
return {
withUrl: (url) => {
return {
build: () => {}
};
}
};
}
};
}
const SIGNALR_PROVIDER = [ { provide: SIGNALR_TOKEN, useFactory: signalRFactory } ];
describe('ConnectionService', () => {
beforeEach(
async(() => {
TestBed.configureTestingModule({
providers: [ SIGNALR_PROVIDER, SignalrService ]
});
})
);
it('should exist', () => {
const connectionService = TestBed.get(SignalrService);
expect(connectionService).toBeTruthy();
});
});
在第二次尝试中,我将模拟对象分配给了一个变量:
import { TestBed, async } from '@angular/core/testing';
import { InjectionToken } from '@angular/core';
import { SignalrService } from './signalr-service';
const SIGNALR_TOKEN = new InjectionToken('signalR');
const SIGNALR_VALUE = {
HubConnectionBuilder: () => {
return {
withUrl: (url) => {
return {
build: () => {}
};
}
};
}
};
const SIGNALR_PROVIDER = [ { provide: SIGNALR_TOKEN, useValue: SIGNALR_VALUE } ];
describe('ConnectionService', () => {
beforeEach(
async(() => {
TestBed.configureTestingModule({
providers: [ SIGNALR_PROVIDER, SignalrService ]
});
})
);
it('should exist', () => {
const connectionService = TestBed.get(SignalrService);
expect(connectionService).toBeTruthy();
});
});
在我的第三次尝试中,我取出了SIGNALR_PROVIDER,并尝试通过工厂和直接值直接在providers数组中提供值:
直接值:
import { TestBed, async } from '@angular/core/testing';
import { InjectionToken } from '@angular/core';
import { SignalrService } from './signalr-service';
const SIGNALR_TOKEN = new InjectionToken('signalR');
const SIGNALR_VALUE = {
HubConnectionBuilder: () => {
return {
withUrl: (url) => {
return {
build: () => {}
};
}
};
}
};
describe('ConnectionService', () => {
beforeEach(
async(() => {
TestBed.configureTestingModule({
providers: [ { provide: SIGNALR_TOKEN, useValue: SIGNALR_VALUE }, SignalrService ]
});
})
);
it('should exist', () => {
const connectionService = TestBed.get(SignalrService);
expect(connectionService).toBeTruthy();
});
});
工厂:
import { TestBed, async } from '@angular/core/testing';
import { InjectionToken } from '@angular/core';
import { SignalrService } from './signalr-service';
const SIGNALR_TOKEN = new InjectionToken('signalR');
function signalRFactory() {
return {
HubConnectionBuilder: () => {
return {
withUrl: (url) => {
return {
build: () => {}
};
}
};
}
};
}
describe('ConnectionService', () => {
beforeEach(
async(() => {
TestBed.configureTestingModule({
providers: [ { provide: SIGNALR_TOKEN, useFactory: signalRFactory }, SignalrService ]
});
})
);
it('should exist', () => {
const connectionService = TestBed.get(SignalrService);
expect(connectionService).toBeTruthy();
});
});
每次尝试仍然给我NullInjectorError: No provider for InjectionToken signalR!
,我被困住了。很明显,我不了解InjectionToken。有人能指出我正确的方向吗?
答案 0 :(得分:3)
弄清楚了-我的问题是我在测试文件中重新定义了令牌,因此我的测试是为我的模拟对象提供一个与服务期望的令牌完全不同的令牌。将其更改为此固定它:
PyMODINIT_FUNC PyInit_NESulator(void)
{
return PyModule_Create(&NESulator_module);
}