抛出异常并在 .net 中的 xunit 断言中获取它
Throw exception and getting it in assert of xunit in .net
我想测试这个方法
public async Task<GetLayoutResponse> Handle(GetLayoutQuery request, CancellationToken cancellationToken)
{
string ravenId = UserLayout.GetId(_userService.CustomerIsin);
var cRepository = CacheableRepository<UserLayout>.From(_repository);
var res = await cRepository.GetAsync(ravenId);
if (!res.IsSucceeded)
throw new EasyException(EasyException.DATABASE_EXCEPTION, "DatabaseException");
return new GetLayoutResponse { LayoutModel=res?.Data?.LayoutModel };
}
正如您在这部分看到的那样throw new EasyException(EasyException.DATABASE_EXCEPTION, "DatabaseException");
我抛出了一个异常。
所以这是我的测试代码:
[Fact]
public async void GetLayoutQueryTestException()
{
//Arrange
var data = new domain.Entities.UserLayout() { CreateDateTime=DateTime.Now, Id= $"{nameof(UserLayout)}/", LayoutModel="MyLayout" };
var mediator = new Mock<IMediator>();
var userservice = new Mock<ICurrentUserService>();
var repoacc = new Mock<IRepositoryAccessor>();
var repo = new Mock<domain.Interfaces.IRepository<UserLayout>>();
repoacc.Setup(i => i.GetRepository<UserLayout>(It.IsAny<string>(), It.IsAny<DatabaseType>(), It.IsAny<Type>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(repo.Object);
repo.Setup(i => i.GetByIdAsync(It.IsAny<string>())).Returns(Task.FromResult(OperationResult<UserLayout>.Failed(EasyException.DATABASE_EXCEPTION.ToString())));
GetLayoutQuery command = new GetLayoutQuery();
GetLayoutQueryHandler handler = new GetLayoutQueryHandler(userservice.Object, repoacc.Object);
//Act
var x = await handler.Handle(command, new System.Threading.CancellationToken());
//Assert
Assert.IsType<EasyException>(x);
}
但是我的测试代码无法检测到异常和returns这个错误:
easy.api.tests.LayoutTests.LayoutResponseTests.GetLayoutQueryTestException
Source: LayoutResponseTests.cs line 89
Duration: 272 ms
Message:
domain.Exceptions.EasyException : DatabaseException
Stack Trace:
GetLayoutQueryHandler.Handle(GetLayoutQuery request, CancellationToken cancellationToken) line 33
LayoutResponseTests.GetLayoutQueryTestException() line 109
<>c.<ThrowAsync>b__127_0(Object state)
最后:
var caughtException =await Assert.ThrowsAsync<EasyException>(() => handler.Handle(command, new System.Threading.CancellationToken()));
Assert.Equal("DatabaseException", caughtException.Message);
我想测试这个方法
public async Task<GetLayoutResponse> Handle(GetLayoutQuery request, CancellationToken cancellationToken)
{
string ravenId = UserLayout.GetId(_userService.CustomerIsin);
var cRepository = CacheableRepository<UserLayout>.From(_repository);
var res = await cRepository.GetAsync(ravenId);
if (!res.IsSucceeded)
throw new EasyException(EasyException.DATABASE_EXCEPTION, "DatabaseException");
return new GetLayoutResponse { LayoutModel=res?.Data?.LayoutModel };
}
正如您在这部分看到的那样throw new EasyException(EasyException.DATABASE_EXCEPTION, "DatabaseException");
我抛出了一个异常。
所以这是我的测试代码:
[Fact]
public async void GetLayoutQueryTestException()
{
//Arrange
var data = new domain.Entities.UserLayout() { CreateDateTime=DateTime.Now, Id= $"{nameof(UserLayout)}/", LayoutModel="MyLayout" };
var mediator = new Mock<IMediator>();
var userservice = new Mock<ICurrentUserService>();
var repoacc = new Mock<IRepositoryAccessor>();
var repo = new Mock<domain.Interfaces.IRepository<UserLayout>>();
repoacc.Setup(i => i.GetRepository<UserLayout>(It.IsAny<string>(), It.IsAny<DatabaseType>(), It.IsAny<Type>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(repo.Object);
repo.Setup(i => i.GetByIdAsync(It.IsAny<string>())).Returns(Task.FromResult(OperationResult<UserLayout>.Failed(EasyException.DATABASE_EXCEPTION.ToString())));
GetLayoutQuery command = new GetLayoutQuery();
GetLayoutQueryHandler handler = new GetLayoutQueryHandler(userservice.Object, repoacc.Object);
//Act
var x = await handler.Handle(command, new System.Threading.CancellationToken());
//Assert
Assert.IsType<EasyException>(x);
}
但是我的测试代码无法检测到异常和returns这个错误:
easy.api.tests.LayoutTests.LayoutResponseTests.GetLayoutQueryTestException
Source: LayoutResponseTests.cs line 89
Duration: 272 ms
Message:
domain.Exceptions.EasyException : DatabaseException
Stack Trace:
GetLayoutQueryHandler.Handle(GetLayoutQuery request, CancellationToken cancellationToken) line 33
LayoutResponseTests.GetLayoutQueryTestException() line 109
<>c.<ThrowAsync>b__127_0(Object state)
最后:
var caughtException =await Assert.ThrowsAsync<EasyException>(() => handler.Handle(command, new System.Threading.CancellationToken()));
Assert.Equal("DatabaseException", caughtException.Message);