gpt4 book ai didi

c# - 使用 NSubstitute 模拟一个方法两次,首先抛出错误,然后为同一个调用的签名请求返回值

转载 作者:行者123 更新时间:2023-12-02 02:46:55 26 4
gpt4 key购买 nike

我想用 NSubstitute 模拟一个方法两次,首先抛出错误,然后为同一个调用的签名请求返回值。

public interface IMyAction
{
public Task<MyDto> Upsert(CreateRequest requestDto);
}

public class MyAction : IMyAction
{

private readonly IMyRepository _repository;

public MyAction(IMyRepository repository)
{
_repository = repository;
}

public Task<MyDto> Upsert(CreateRequest requestDto){
{
var domainEntity = await _repository.Get(requestDto.Param1);
if(domainEntity == null)
{
try
{
// try to create entity
}
catch(RecordExistsException) // Throws when another concurrent request creates the same entity. Need to test this catch block scenario
{
domainEntity = await _repository.Get(requestDto.Param1);
//
//...perform update operation using the domainEntity and requestDto
//
}
catch(Exception ex)
{
throw
}
}
}
}

我有一个边缘情况,我希望第一次调用应该抛出异常,第二次调用应该返回 dto。两个调用的参数值相同。

我正在使用 NSubstitute 模拟 xunit 测试中的依赖关系。

我的设置:

IMyRepository _repository = Substitute.For<IMyRepository>();
var myAction = new MyAction(_repository);

_repository.Get(Arg.Any<string>()).Throws<NotFoundException>();
_repository.When(x => x.Get(Arg.Is<string>(r => r == "id1")))
.Do(x => _repository.Get(Arg.Is<string>(r => r == "id1")).Returns(new MyDto()));

期望:

_repository.Get("id1").Throws<NotFoundException>(); // first call invocation
_repository.Get("id1").Returns(new MyDto()); // second call invocation

但是当调用 domainEntitymyAction.Upsert(new CreateRequest()); 在第一次调用中返回而不是抛出异常。

最佳答案

深入研究 SO 后,我找到了一个解决方案 here .

对于我的情况,我通过如下模拟解决了这个问题-

_repository.Get("id1").Returns(x => throw new NotFoundException(), x => new MyDto());

Returns() 还支持传递多个函数以从中返回,这允许序列中的一个调用抛出异常或执行其他操作。

关于c# - 使用 NSubstitute 模拟一个方法两次,首先抛出错误,然后为同一个调用的签名请求返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62640655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com