用于测试的 IHostingEnvironment
IHostingEnvironment for testing
我正在尝试使用 ABP 2.0.2 中的单元测试项目,当我 运行 选择测试 GetUsers_Test().
Message: Castle.MicroKernel.Handlers.HandlerException : Can't create component 'imfundoplatform.imfundoplatformCoreModule' as it has dependencies to be satisfied.
'imfundoplatform.imfundoplatformCoreModule' is waiting for the following dependencies:
- Service 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' which was not registered.
我的 Core 模块的构造函数:
public imfundoplatformCoreModule(IHostingEnvironment env)
{
_appConfiguration = AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName, env.IsDevelopment());
}
我不知道如何将它传递给模块或让单元测试正常工作。请帮忙!
您不能注入 IHostingEnvironment ... 要获取内容根路径,请使用;
Directory.GetCurrentDirectory
您可以 注入 IHostingEnvironment。但是你将不得不以一些奇怪的方式来做:
首先像这样创建一个模拟 IHostingEnvrionment class(根据您的需要进行调整):
public class MockHostingEnvironment : IHostingEnvironment, ISingletonDependency
{
public string EnvironmentName
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public string ApplicationName
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public string WebRootPath { get; set; } = Path.Combine(Environment.CurrentDirectory, "wwwroot");
public IFileProvider WebRootFileProvider
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public string ContentRootPath { get; set; } = Environment.CurrentDirectory;
public IFileProvider ContentRootFileProvider
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
}
之后将此添加到您的 TestModule 的 Initialize()
:
public override void Initialize()
{
(...)
IocManager.Register<IHostingEnvironment, MockHostingEnvironment>(DependencyLifeStyle.Singleton);
}
请注意,使用 Environment.CurrentDirectory 是一种非常糟糕的做法。它可能指向不同的目录,具体取决于您的 CI、您的测试运行器、您的测试框架等。
如果您在测试中使用需要 IHostingEnvironment 的服务,您应该仅 使用此 MockHostingEnvironment。
我正在尝试使用 ABP 2.0.2 中的单元测试项目,当我 运行 选择测试 GetUsers_Test().
Message: Castle.MicroKernel.Handlers.HandlerException : Can't create component 'imfundoplatform.imfundoplatformCoreModule' as it has dependencies to be satisfied.
'imfundoplatform.imfundoplatformCoreModule' is waiting for the following dependencies:
- Service 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' which was not registered.
我的 Core 模块的构造函数:
public imfundoplatformCoreModule(IHostingEnvironment env)
{
_appConfiguration = AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName, env.IsDevelopment());
}
我不知道如何将它传递给模块或让单元测试正常工作。请帮忙!
您不能注入 IHostingEnvironment ... 要获取内容根路径,请使用;
Directory.GetCurrentDirectory
您可以 注入 IHostingEnvironment。但是你将不得不以一些奇怪的方式来做:
首先像这样创建一个模拟 IHostingEnvrionment class(根据您的需要进行调整):
public class MockHostingEnvironment : IHostingEnvironment, ISingletonDependency
{
public string EnvironmentName
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public string ApplicationName
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public string WebRootPath { get; set; } = Path.Combine(Environment.CurrentDirectory, "wwwroot");
public IFileProvider WebRootFileProvider
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public string ContentRootPath { get; set; } = Environment.CurrentDirectory;
public IFileProvider ContentRootFileProvider
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
}
之后将此添加到您的 TestModule 的 Initialize()
:
public override void Initialize()
{
(...)
IocManager.Register<IHostingEnvironment, MockHostingEnvironment>(DependencyLifeStyle.Singleton);
}
请注意,使用 Environment.CurrentDirectory 是一种非常糟糕的做法。它可能指向不同的目录,具体取决于您的 CI、您的测试运行器、您的测试框架等。
如果您在测试中使用需要 IHostingEnvironment 的服务,您应该仅 使用此 MockHostingEnvironment。