TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)

时间:2018-04-23 12:30:04

标签: c# unit-testing moq mstest

我对MSTest很新,我在运行以下测试时遇到以下错误。尽管如此,我无法从异常中做出太多贡献。

请求您的帮助!我还附上了下面的测试方法代码。

enter image description here

[TestMethod]
public async Task GetReturnsAllSolutionsForAdminUser()
{
    var storageService = new Mock<IStorageService>();
    AuthenticatedUsername = "xxx@yyy.com";

    var mockStorage = new Mock<IStorageService>();
    var mockGeneralServices = new Mock<IGeneralServices>();
    var mockDesignerEngine = new Mock<IDesignerEngine>();
    var mockExcelUploadService = new Mock<ISolutionBuilderFromExcelService>();


    var solutions = new List<Solution>(
        new[]
        {
            new Solution
            {
                Id = "10000000-0000-0000-0000-000000000000",
                Name = "Solution 1",
                Description = "Description Of Solution 1",
                ResourceGroups = new NamedItemList<ResourceGroup>(new[] {new ResourceGroup{Name="Solution 1:ResourceGroup 1"}}),
                CreatedBy = "xxx@yyy.com",
                CreatedOn = DateTime.Parse("05/05/2018"),
                Version = "1.0.1",
                TaggingEnabled = true,
            }
        }
    );
    storageService.Setup(it => it.ReadItemsAsync<Solution>()).ReturnsAsync(solutions);


    var controller = new SolutionsController(mockGeneralServices.Object, mockStorage.Object, mockDesignerEngine.Object, mockExcelUploadService.Object)
    {
        ControllerContext = new ControllerContext
        {
            HttpContext = GetMockHttpContext(CloudSuiteRoles.AdminRole, CloudSuiteRoles.UserRole).Object
        }
    };

    var result = await controller.Get().ConfigureAwait(false) as JsonResult;
    var items = result.Value as IEnumerable<dynamic>;//
    Assert.IsNotNull(items);
    Assert.AreEqual(solutions.Count(), items.Count());
}

1 个答案:

答案 0 :(得分:1)

没有看到storageService在测试中的使用位置以及它与被测对象的关系。

您很可能打算Setup mockStorage

替换

storageService.Setup(it => it.ReadItemsAsync<Solution>()).ReturnsAsync(solutions);

mockStorage.Setup(_ => _.ReadItemsAsync<Solution>()).ReturnsAsync(solutions);

因为那是被注入被测试控制器的那个。

错误是因为调用的asyc方法尚未被模拟,并且该方法无法流向完成。