我正在尝试对我正在构建的类进行单元测试,该类调用多个 URL(异步)并检索内容。
这是我遇到问题的测试:
[Test]
public void downloads_content_for_each_url()
{
_mockGetContentUrls.Setup(x => x.GetAll())
.Returns(new[] { "http://www.url1.com", "http://www.url2.com" });
_mockDownloadContent.Setup(x => x.DownloadContentFromUrlAsync(It.IsAny<string>()))
.Returns(new Task<IEnumerable<MobileContent>>(() => new List<MobileContent>()));
var downloadAndStoreContent= new DownloadAndStoreContent(
_mockGetContentUrls.Object, _mockDownloadContent.Object);
downloadAndStoreContent.DownloadAndStore();
_mockDownloadContent.Verify(x => x.DownloadContentFromUrlAsync("http://www.url1.com"));
_mockDownloadContent.Verify(x => x.DownloadContentFromUrlAsync("http://www.url2.com"));
}
相关部分DownloadContent
有:
public void DownloadAndStore()
{
//service passed in through ctor
var urls = _getContentUrls.GetAll();
var content = DownloadAll(urls)
.Result;
//do stuff with content here
}
private async Task<IEnumerable<MobileContent>> DownloadAll(IEnumerable<string> urls)
{
var list = new List<MobileContent>();
foreach (var url in urls)
{
var content = await _downloadMobileContent.DownloadContentFromUrlAsync(url);
list.AddRange(content);
}
return list;
}
当我的测试运行时,它永远不会完成 - 它只是挂起。
我怀疑我的设置中的某些东西_mockDownloadContent
是罪魁祸首......