大佬教程收集整理的这篇文章主要介绍了XUnit Mock 没有像我期望的那样抛出异常,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下使用类模拟的测试。当我尝试抛出异常时,它实际上从未抛出异常。就好像该方法实际上正在被调用一样,我不确定为什么。
测试如下:
[Fact]
public async Task ReadResultSetShouldRetry()
{
// Arrange
_cosmosUtilWrapper.Setup(x => x.ReadCosmosResultSet<CosmosRepositoryTests>(It.IsAny<FeedIterator>(),It.IsAny<ILogger>(),It.IsAny<CancellationToken>()))
.Throws(new Exception("It broke"));
var cosmosReadPolicy = new CosmosReadPolicy();
// Act
await Assert.ThrowsAsync<Exception>(async () => await CosmosRepository.ReadCosmosResultSetWithRetry<CosmosRepositoryTests>(_mockFeedIterator.object,_logger,cosmosReadPolicy,CancellationToken.NonE));
// Assert
_cosmosUtilWrapper.Verify(x => x.ReadCosmosResultSet<CosmosRepositoryTests>(_mockFeedIterator.object,default));
}
这是它正在调用的方法,我将它封装在重试策略中:
public static async Task<List<T>> ReadCosmosResultSetWithRetry<T>(
FeedIterator resultSet,ILogger logger,CosmosReadPolicy cosmosReadPolicy,CancellationToken cancellationToken = default)
where T : class
{
CosmosUtilWrapper utilWrapper = new CosmosUtilWrapper();
var ReadCosmosResultSet = await cosmosReadPolicy.GetPolicy.ExecuteAsync(async () => await utilWrapper.ReadCosmosResultSet<T>(resultSet,logger,cancellationToken));
return ReadCosmosResultSet;
}
这里是 CosmosUtilWrapper,下面是实际的 Cosmos Util 类:
public class CosmosUtilWrapper
{
public virtual async Task<List<T>> ReadCosmosResultSet<T>(
FeedIterator resultSet,CancellationToken cancellationToken = default)
where T : class
{
return await CosmosUtil.ReadCosmosResultSet<T>(resultSet,cancellationToken);
}
}
这是在上述类中返回的实际静态 Util 方法。不得不这样做,因为这个类是一个静态类,并且单元测试不是很有趣。
public static async Task<List<T>> ReadCosmosResultSet<T>(
FeedIterator resultSet,CancellationToken cancellationToken = default)
where T : class
{
var founddocuments = new List<T>();
while (resultSet.HasMoreResults)
{
using Responsemessage responsemessage = await resultSet.ReadNextAsync(cancellationToken);
if (responsemessage.IssuccessstatusCodE)
{
using StreamReader streamReader = new StreamReader(responsemessage.Content);
using JsontextReader textReader = new JsontextReader(streamReader);
founddocuments.AddRange((await JObject.LoadAsync(textReader,cancellationToken)).GetValue("documents").ToObject<List<T>>());
}
else
{
throw new Exception($"Unable to read cosmos result set. Status code: {responsemessage.StatusCodE}");
}
}
return founddocuments;
}
最后,这是我在运行测试时得到的消息
message:
Assert.Throws() Failure
Expected: typeof(System.Exception)
Actual: (No exception was thrown)
这是因为 _cosmosUtilWrapper
中的模型对象 ReadResultSetShouldRetry()
从未在 Task<List<T>> ReadCosmosResultSetWithRetry<T>
中使用过。
在方法 Task<List<T>> ReadCosmosResultSetWithRetry<T>
中,您初始化一个新对象 CosmosUtilWrapper utilWrapper = new CosmosUtilWrapper();
。所以,这个对象和上面的对象不一样。
您可以使用以下代码从模型对象中获取对象:_cosmosUtilWrapper.object
。当您从方法中删除静态时,将此对象传递到函数或类的构造函数中。
例如:
public static async Task<List<T>> ReadCosmosResultSetWithRetry<T>(
FeedIterator resultSet,ILogger logger,CosmosReadPolicy cosmosReadPolicy,CosmosUtilWrapper utilWrapper,CancellationToken cancellationToken = default)
where T : class
{
var ReadCosmosResultSet = await cosmosReadPolicy.GetPolicy.ExecuteAsync(async () => await utilWrapper.ReadCosmosResultSet<T>(resultSet,logger,cancellationToken));
return ReadCosmosResultSet;
}
例如测试:
[Fact]
public async Task ReadResultSetShouldRetry()
{
// Arrange
_cosmosUtilWrapper.Setup(x => x.ReadCosmosResultSet<CosmosRepositoryTests>(It.IsAny<FeedIterator>(),It.IsAny<ILogger>(),It.IsAny<CancellationToken>()))
.Throws(new Exception("It broke"));
var cosmosReadPolicy = new CosmosReadPolicy();
var utilWrapper = _cosmosUtilWrapper.object;
// Act
await Assert.ThrowsAsync<Exception>(async () => await CosmosRepository.ReadCosmosResultSetWithRetry<CosmosRepositoryTests>(_mockFeedIterator.object,_logger,cosmosReadPolicy,utilWrapper,CancellationToken.NonE));
// Assert
_cosmosUtilWrapper.Verify(x => x.ReadCosmosResultSet<CosmosRepositoryTests>(_mockFeedIterator.object,default));
}
以上是大佬教程为你收集整理的XUnit Mock 没有像我期望的那样抛出异常全部内容,希望文章能够帮你解决XUnit Mock 没有像我期望的那样抛出异常所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。