TestMethod 未在 c# 中运行

1 .net c# testing integration-testing

我有以下测试类:

using System;
using DDDSample1.Domain.DriverDuties;
using DDDSample1.Domain.Shared;
using DDDSample1.Controllers;
using System.Collections.Generic;
using Moq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IntegrationTests
{
    public class DriverDutyControllerTest
    {
        [TestMethod]
        public async void GetByIdTest()
        {
            var repo = new Mock<IDriverDutyRepository>();
            var unitOfWork = new Mock<IUnitOfWork>();

            var service = new DriverDutyService(unitOfWork.Object, repo.Object);
            var controller = new DriverDutiesController(service);

            DriverDutyId id = new DriverDutyId("id1");
            string key = "keyDD1";
            string driver = "DriverDD";
            List<String> workblocks = new List<String>();
            workblocks.Add("wb1");
            workblocks.Add("wb2");
            workblocks.Add("wb3");

            var ddDto = new CreatingDriverDutyDto(key, driver, workblocks.ToArray());
            var dd = new DriverDuty(key, driver, workblocks);

            repo.Setup(_ => _.AddAsync(dd)).ReturnsAsync(dd);

            var actual = await controller.GetGetById(id.AsGuid());

            Console.WriteLine(actual.Value);

            Assert.IsTrue(true);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在 Visual Studio Code 中运行它,但每当我运行测试时,GetByIdTest()我都会得到以下输出:

using System;
using DDDSample1.Domain.DriverDuties;
using DDDSample1.Domain.Shared;
using DDDSample1.Controllers;
using System.Collections.Generic;
using Moq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IntegrationTests
{
    public class DriverDutyControllerTest
    {
        [TestMethod]
        public async void GetByIdTest()
        {
            var repo = new Mock<IDriverDutyRepository>();
            var unitOfWork = new Mock<IUnitOfWork>();

            var service = new DriverDutyService(unitOfWork.Object, repo.Object);
            var controller = new DriverDutiesController(service);

            DriverDutyId id = new DriverDutyId("id1");
            string key = "keyDD1";
            string driver = "DriverDD";
            List<String> workblocks = new List<String>();
            workblocks.Add("wb1");
            workblocks.Add("wb2");
            workblocks.Add("wb3");

            var ddDto = new CreatingDriverDutyDto(key, driver, workblocks.ToArray());
            var dd = new DriverDuty(key, driver, workblocks);

            repo.Setup(_ => _.AddAsync(dd)).ReturnsAsync(dd);

            var actual = await controller.GetGetById(id.AsGuid());

            Console.WriteLine(actual.Value);

            Assert.IsTrue(true);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么我的测试被跳过?关于如何解决它有什么想法吗?

注意:我正在层(在本例中为控制器和服务)之间开发集成测试,因此我需要模拟这些层,因为我不想访问数据库。

mm8*_*mm8 5

尝试将签名从更改async voidasync Task

[TestMethod]
public async Task GetByIdTest()
...
Run Code Online (Sandbox Code Playgroud)

void测试运行程序无法等待方法返回。

另外,该类应该用以下[TestClass]属性修饰:

[TestClass]
public class DriverDutyControllerTest
...
Run Code Online (Sandbox Code Playgroud)