C# 如何使用Moq为异步函数引发异常
我正在使用xUnit和Moq编写测试用例 我在测试类中使用下面的代码来测试另一个类方法的C# 如何使用Moq为异步函数引发异常,c#,unit-testing,.net-core,moq,xunit,C#,Unit Testing,.net Core,Moq,Xunit,我正在使用xUnit和Moq编写测试用例 我在测试类中使用下面的代码来测试另一个类方法的catch() private readonly IADLS_Operations _iADLS_Operations; [Fact] public void CreateCSVFile_Failure() { var dtData = new DataTable(); string fileName = ""; var mockClient = new Mock<IHtt
catch()
private readonly IADLS_Operations _iADLS_Operations;
[Fact]
public void CreateCSVFile_Failure()
{
var dtData = new DataTable();
string fileName = "";
var mockClient = new Mock<IHttpHandler>();
this._iADLS_Operations = new ADLS_Operations(mockClient.Object);
mockClient.Setup(repo => repo.PostAsync(It.IsAny<string>(), It.IsAny<HttpContent>(), It.IsAny<string>()))
.Returns(() => Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest)));
mockClient.Setup(repo => repo.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<string>()))
.Returns(() => Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest))); // here I want to return Exception instead of BadRequest. How to do that.
Exception ex = Assert.Throws<Exception>(() => this._iADLS_Operations.CreateCSVFile(dtData, fileName).Result);
Assert.Contains("Exception occurred while executing method:", ex.Message);
}
如何实现这一点。正如@Johnny在评论中提到的,您可以将代码中的
返回
替换为抛出
,如:
mockClient.Setup(repo => repo.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<string>())).Throws(new Exception("exception message"));
mockClient.Setup(repo=>repo.SendAsync(It.IsAny(),It.IsAny()).Throws(新异常(“异常消息”));
此外,您还可以抛出如下异常:
mockClient.Setup(repo => repo.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<string>())).Throws<InvalidOperationException>();
mockClient.Setup(repo=>repo.sendsync(It.IsAny(),It.IsAny()).Throws();
您可以找到有关抛出异常和moq的更多信息。考虑到被测代码的异步性质,如果测试代码也是异步的,那就更好了。Moq具有异步功能
[Fact]
public async Task CreateCSVFile_Failure() {
//Arrange
var dtData = new DataTable();
string fileName = "";
var mockClient = new Mock<IHttpHandler>();
this._iADLS_Operations = new ADLS_Operations(mockClient.Object);
mockClient
.Setup(repo => repo.PostAsync(It.IsAny<string>(), It.IsAny<HttpContent>(), It.IsAny<string>()))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest));
mockClient
.Setup(repo => repo.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<string>()))
.ThrowsAsync(new Exception("Some message here"));
//Act
Func<Task> act = () => this._iADLS_Operations.CreateCSVFile(dtData, fileName);
//Assert
Exception ex = await Assert.ThrowsAsync<Exception>(act);
Assert.Contains("Exception occurred while executing method:", ex.Message);
}
[事实]
公共异步任务CreateCSVFile_失败(){
//安排
var dtData=新数据表();
字符串fileName=“”;
var mockClient=new Mock();
这是。_iADLS_Operations=新的ADLS_操作(mockClient.Object);
模拟客户端
.Setup(repo=>repo.PostAsync(It.IsAny(),It.IsAny(),It.IsAny())
.ReturnsAsync(新的HttpResponseMessage(HttpStatusCode.BadRequest));
模拟客户端
.Setup(repo=>repo.sendsync(It.IsAny(),It.IsAny())
.ThrowsAsync(新异常(“此处的某些消息”);
//表演
Func act=()=>this.\u iADLS\u Operations.CreateCSVFile(dtData,fileName);
//断言
Exception ex=await Assert.ThrowsAsync(act);
Contains(“执行方法时发生异常:”,例如Message);
}
注意在设置中使用了Moq的ReturnsAsync
和ThrowsAsync
,以及xUnit的Assert.ThrowsAsync
现在,您可以避免执行诸如
.Result
之类的阻塞调用,这可能会导致死锁。抛出而不是返回
…感谢您的建议。当我遇到麻烦时,StackOverflow从不让我失望。极好的解释。问题解决了。
[Fact]
public async Task CreateCSVFile_Failure() {
//Arrange
var dtData = new DataTable();
string fileName = "";
var mockClient = new Mock<IHttpHandler>();
this._iADLS_Operations = new ADLS_Operations(mockClient.Object);
mockClient
.Setup(repo => repo.PostAsync(It.IsAny<string>(), It.IsAny<HttpContent>(), It.IsAny<string>()))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest));
mockClient
.Setup(repo => repo.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<string>()))
.ThrowsAsync(new Exception("Some message here"));
//Act
Func<Task> act = () => this._iADLS_Operations.CreateCSVFile(dtData, fileName);
//Assert
Exception ex = await Assert.ThrowsAsync<Exception>(act);
Assert.Contains("Exception occurred while executing method:", ex.Message);
}