【发布时间】:2020-06-22 15:26:29
【问题描述】:
我有一个处理打印的小型 C# 类。 我想为这个类创建(n)单元测试,使用 假易。我怎么能伪造这个的内部调用 在不伪造整个 SUT 的情况下上课?
例如:
public class MyPrintHandler: IMyPrintHandler
{
public MyPrintHandler(ILogger<MyPrintHandler> logger)
{
}
// function I want to (unit test
public async Task<bool> PrintAsync(string ipaddress)
{
try
{
if (!string.IsNullOrWhiteSpace(ipaddress) )
{
return await StartPrint(ipaddress); // This cannot be called in a unit test, because it really start printing on a printer.
}
}
catch (Exception e)
{
}
return false;
}
private async Task<bool> StartPrint(string ipaddress)
{
// prints on the printer
}
[TestFixture]
public class MyPrintHandlerTests
{
[Test]
public void Succes_PrintAsync()
{
using (var fake = new AutoFake())
{
// Arrange - configure the fake
var sut = fake.Resolve<MyPrintHandler>();
// Act
await sut.PrintAsync("0.0.0.0"); // I want to prevent StartPrint() from being called..
}
}
}
我怎样才能做到这一点,或者这根本不可能? 提前致谢!
【问题讨论】:
标签: unit-testing nunit fakeiteasy