我正在尝试为下面的类编写集成测试,但是我坚持在测试中获取IHostingEnvironment。我假设我应该能够从TestServer获得IHostingEnvironment,但没有骰子。我缺少基本的东西吗?比集成测试更适合单元测试吗?
实际课程
public class PasswordResetMessage : IMessage
{
private readonly PasswordReset passwordReset;
private readonly IHostingEnvironment iHostingEnvironment;
public PasswordResetMessage(IHostingEnvironment iHostingEnvironment, PasswordReset passwordReset)
{
this.passwordReset = passwordReset;
this.iHostingEnvironment=iHostingEnvironment;
}
public string Get()
{
var stringBuilder = new StringBuilder();
var pathToFile = Path.Combine(iHostingEnvironment.ContentRootPath, "EmailTemplates", "password_reset.txt");
using (var streamReader = File.OpenText(pathToFile))
{
stringBuilder.Append(streamReader.ReadToEnd());
}
return stringBuilder.Replace("{{ActionURL}}", passwordReset.ActionURL).ToString();
}
}
集成测试:
public class PasswordResetMessageTest
{
private PasswordResetMessage passwordResetMessage;
private readonly TestServer testServer;
public PasswordResetMessageTest()
{
testServer = new TestServer(new WebHostBuilder().UseStartup<Startup>());
passwordResetMessage = new PasswordResetMessage(**<IDon'tKnowWhatToPassHere>**, new PasswordReset());
}
[Fact]
public void Get()
{
var message=passwordResetMessage.Get();
Assert.Contains(message, "");
}
}