我正在测试这家工厂
public class ContratoFactory : IContratoFactory
{
private readonly IContratoPodeSerCriadoValidation _contratoPodeSerCriadoValidation;
public ContratoFactory(IContratoPodeSerCriadoValidation contratoPodeSerCriadoValidation)
{
_contratoPodeSerCriadoValidation = contratoPodeSerCriadoValidation;
}
public async Task<IValidationResult> Build(Contrato entity)
{
try
{
var result = _contratoPodeSerCriadoValidation.Valid(entity);
}
catch (Exception ex)
{
// throw
}
}
public async Task AdicionarLocalBase(Contrato entity)
{
}
}
这是我的测试类
[TestClass]
public class ContratoFactoryTests
{
private IContratoPodeSerCriadoValidation _contratoPodeSerCriadoValidation;
private ContratoFactory _contratoFactory;
[TestInitialize]
public void Initialize()
{
_contratoPodeSerCriadoValidation = Substitute.For<IContratoPodeSerCriadoValidation>();
_contratoFactory = new ContratoFactory(_contratoPodeSerCriadoValidation);
}
[TestMethod]
public async Task AoSerCriadoOContratoDeveSerValidado()
{
var contrato = ContratoValues.ContratoComEmpresaENomeValido;
await _contratoFactory.Build(contrato);
_contratoPodeSerCriadoValidation.Valid(contrato).Received();
}
}
当我运行我的测试时,它们没有运行,或者返回一个&#34; Inconclusive&#34;结果
如何使用NSubstitute和FluentAssertions测试异步调用?
更新19/05/16
我修复了测试方法以返回Task而不是void并且测试抛出NullReferenceExceptions
答案 0 :(得分:2)
我只是复制/粘贴你的代码,并为缺少的类型添加了几个空接口和类。测试开箱即用。
关于你的问题,&#34;如何使用NSubstitute和FluentAssertions测试异步调用?&#34;,以下是如何从异步方法检查异常。
Func<Task> action = async () => await _contratoFactory.Build(contrato);
action.ShouldThrow<SomeValidationFailedException>();