mva*_*lla 163 c# unit-testing asynchronous moq
我正在测试一个进行Web API调用的服务的方法.HttpClient如果我还在本地运行Web服务(位于解决方案中的另一个项目中),则使用正常工作正常进行单元测试.
但是,当我签入我的更改时,构建服务器将无法访问Web服务,因此测试将失败.
我已经为我的单元测试设计了一种方法,通过创建一个IHttpClient接口并实现我在我的应用程序中使用的版本.对于单元测试,我使用模拟的异步post方法创建一个模拟版本.这是我遇到问题的地方.我想HttpStatusResult为这个特定的测试返回一个OK .对于另一个类似的测试,我将返回一个糟糕的结果.
测试将运行但永远不会完成.它挂在等待.我是异步编程,代表和Moq本身的新手,我一直在搜索SO和谷歌一段时间学习新东西,但我似乎仍然无法解决这个问题.
这是我试图测试的方法:
public async Task<bool> QueueNotificationAsync(IHttpClient client, Email email)
{
// do stuff
try
{
// The test hangs here, never returning
HttpResponseMessage response = await client.PostAsync(uri, content);
// more logic here
}
// more stuff
}
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试方法:
[TestMethod]
public async Task QueueNotificationAsync_Completes_With_ValidEmail()
{
Email email = new Email()
{
FromAddress = "bob@example.com",
ToAddress = "bill@example.com",
CCAddress = "brian@example.com",
BCCAddress = "ben@example.com",
Subject = "Hello",
Body = "Hello World."
};
var mockClient = new Mock<IHttpClient>();
mockClient.Setup(c => c.PostAsync(
It.IsAny<Uri>(),
It.IsAny<HttpContent>()
)).Returns(() => new Task<HttpResponseMessage>(() => new HttpResponseMessage(System.Net.HttpStatusCode.OK)));
bool result = await _notificationRequestService.QueueNotificationAsync(mockClient.Object, email);
Assert.IsTrue(result, "Queue failed.");
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
谢谢您的帮助.
Jon*_*eet 326
你正在创建一个任务但从未启动它,所以它永远不会完成.但是,不要只是启动任务 - 而是更改为使用Task.FromResult<TResult>,这将为您提供已完成的任务:
...
.Returns(Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.OK)));
Run Code Online (Sandbox Code Playgroud)
请注意,您不会以这种方式测试实际的异步 - 如果您想这样做,您需要做更多的工作来创建一个Task<T>您可以以更细粒度的方式控制...但这是适合的另一天.
您可能还想考虑使用假货IHttpClient而不是模仿一切 - 这实际上取决于您需要它的频率.
小智 8
尝试使用ReturnsAsync. 在异步方法中它可以工作,我相信解决你的问题的基础应该是类似的。
_mocker.GetMock<IMyRepository>()
.Setup(x => x.GetAll())
.ReturnsAsync(_myFakeListRepository.GetAll());
Run Code Online (Sandbox Code Playgroud)
在上面推荐@Stuart Grassie的答案。
var moqCredentialMananger = new Mock<ICredentialManager>();
moqCredentialMananger
.Setup(x => x.GetCredentialsAsync(It.IsAny<string>()))
.ReturnsAsync(new Credentials() { .. .. .. });
Run Code Online (Sandbox Code Playgroud)
对于Mock.Of<...>(...)forasync方法,您可以使用Task.FromResult(...):
var client = Mock.Of<IHttpClient>(c =>
c.PostAsync(It.IsAny<Uri>(), It.IsAny<HttpContent>()) == Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
69920 次 |
| 最近记录: |