Phantomjs 使用Aurelia测试时未模拟服务
我试图在Aurelia测试中模拟几个服务,但一个是注入模拟,另一个是注入真正的服务。我看不出有什么区别 我的测试规范中有几个服务模拟:Phantomjs 使用Aurelia测试时未模拟服务,phantomjs,aurelia,jestjs,Phantomjs,Aurelia,Jestjs,我试图在Aurelia测试中模拟几个服务,但一个是注入模拟,另一个是注入真正的服务。我看不出有什么区别 我的测试规范中有几个服务模拟: export class MockCommunicationService { imAmock = true; get(id: number): Promise<ICommunication> { return Promise.resolve({} as ICommunication); } } export
export class MockCommunicationService {
imAmock = true;
get(id: number): Promise<ICommunication> {
return Promise.resolve({} as ICommunication);
}
}
export class MockRoundService {
getRounds(): Promise<IRoundList[]> {
return Promise.resolve([{} as IRoundList]);
}
}
如果未定义imAmock,则此操作失败。如果我console.log(viewModel.communicationService)
我可以看到真正的通信服务及其注入的所有依赖项(如http等)
但是,如果我对RoundService
执行完全相同的操作,那么上面的模拟将如您所期望的那样被注入
viewModel本身对两种服务的使用方式相同:
@autoinject()
export class Communications {
...
constructor(public readonly communicationService: CommunicationService, public readonly roundService: RoundService,
private readonly bindingEngine: BindingEngine, private readonly eventAggregator: EventAggregator, private readonly animator: CssAnimator,
private readonly validationControllerFactory: ValidationControllerFactory) {
...
}
}
(我将前两个公开,以便在测试中访问它们,但它们通常是私有的)
我能找到的唯一提示是Aurelia DI实现使用键映射(通常是类)来解析实例。如果我以某种方式定义了CommunicationService
两次,那么我可能会为同一个类获得两个不同的键。。。但我不知道这会发生什么,也不知道如何解决
非常感谢你的帮助
编辑! 多亏了,我开始思考如何重新订购注册和第一个container.get call,我想出了以下方法:
beforeEach(() => {
container = new Container();
component = StageComponent
.withResources(PLATFORM.moduleName('path/to/real/communications'))
.inView('<communications></communications>')
.boundTo(viewModel);
component.bootstrap(aurelia => {
aurelia.use.standardConfiguration()
.plugin(PLATFORM.moduleName("aurelia-validation"));
aurelia.container.registerInstance(RoundService, roundService);
aurelia.container.registerInstance(CommunicationService, service);
viewModel = aurelia.container.get(Communications);
});
});
beforeach(()=>{
容器=新容器();
组件=阶段组件
.withResources(PLATFORM.moduleName('path/to/real/communications'))
.inView(“”)
.boundTo(视图模型);
引导(aurelia=>{
aurelia.use.standardConfiguration()
.plugin(PLATFORM.moduleName(“aurelia验证”);
aurelia.container.registerInstance(RoundService,RoundService);
aurelia.container.registerInstance(通信服务,服务);
viewModel=aurelia.container.get(通信);
});
});
这是一种享受,两项服务现在都是模拟服务
编辑2
我不确定我之前试过什么,但上面的代码不起作用!因此,我回到了第一步-如何将服务注入到测试中?刚刚查看了它,在我看来,在注册模拟实例之前,您正在从容器中请求通信实例 我没有足够的时间来测试正确的解决方案(如果您提供了要点,我会为您修改)。但是,如果您已经在使用容器获取viewmodel,您能否将这些行移到
容器上方。获取以制作:
container.registerInstance(RoundService, roundService);
container.registerInstance(CommunicationService, service);
viewModel = container.get(Communications);
component = StageComponent
.withResources(PLATFORM.moduleName('path/to/real/communications'))
.inView('<communications></communications>')
.boundTo(viewModel);
component.bootstrap(aurelia => {
aurelia.use.standardConfiguration()
.plugin(PLATFORM.moduleName("aurelia-validation"));
});
container.registerInstance(RoundService,RoundService);
容器注册状态(通信服务、服务);
viewModel=container.get(通信);
组件=阶段组件
.withResources(PLATFORM.moduleName('path/to/real/communications'))
.inView(“”)
.boundTo(视图模型);
引导(aurelia=>{
aurelia.use.standardConfiguration()
.plugin(PLATFORM.moduleName(“aurelia验证”);
});
不完全是,事实上这停止了我简单的expect(true)代码>工作测试。然而,你让我在container.get之后重新排序注册,这是正确的,我对我的问题进行了编辑(给我一点时间来写)。既然你指给我看了,如果你更新你的答案,我很高兴把这个标记为接受。我不知道您的解决方案是如何工作的,因此我不想更新我不理解的问题的答案。我也不知道为什么我的解决方案不起作用。
container.registerInstance(RoundService, roundService);
container.registerInstance(CommunicationService, service);
viewModel = container.get(Communications);
component = StageComponent
.withResources(PLATFORM.moduleName('path/to/real/communications'))
.inView('<communications></communications>')
.boundTo(viewModel);
component.bootstrap(aurelia => {
aurelia.use.standardConfiguration()
.plugin(PLATFORM.moduleName("aurelia-validation"));
});