【问题标题】:TestMethod Not running in c#测试方法未在 C# 中运行
【发布时间】:2021-01-20 20:32:28
【问题描述】:

我有以下测试类:

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);
        }
    }
}

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

----- Running test method "IntegrationTests.DriverDutyControllerTest.GetByIdTest" -----

Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  DDDNetCore -> z:\Git\projeto_integrador_grupo67\mdv\bin\Debug\netcoreapp5.0\DDDNetCore.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.71


----- Test Execution Summary -----

Total tests: 0. Passed: 0. Failed: 0. Skipped: 0

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

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

【问题讨论】:

    标签: c# .net testing integration-testing


    【解决方案1】:

    尝试将签名从async void更改为async Task

    [TestMethod]
    public async Task GetByIdTest()
    ...
    

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

    此外,该类应使用[TestClass] 属性进行修饰:

    [TestClass]
    public class DriverDutyControllerTest
    ...
    

    【讨论】:

      猜你喜欢
      • 2013-08-21
      • 1970-01-01
      • 2018-10-31
      • 2023-03-25
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      相关资源
      最近更新 更多