【问题标题】:C# Unit Test is not recognizing other classesC# 单元测试无法识别其他类
【发布时间】:2020-04-23 11:12:46
【问题描述】:

1st - 帮助社区,如果你给负分,至少说明原因,这样我就不再重复了!

我是一名初学者,我正在尝试测试一个简单的课程。但是我的 UnitTest 无法识别其他命名空间,即使我添加了参考。 See the reference above

我不能使用“使用”指令:

enter image description here

这是一个 .net Core 3.1 项目。 VS 2019 社区

这可能真的很愚蠢,但我被困在其中,并且已经尝试了我所知道的一切。

我要导入的类

namespace DI.BLL
{
    public class ContainerBuilder
    {
        private readonly IList<Type> _registration = new List<Type>();

        public void Register<T>()
        {
            this._registration.Add(typeof(T));
        }

        public Container Build() => new Container(_registration);
    }
}

测试代码:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xunit;
using DI.BLL;

namespace DI.Test
{
    [TestClass]
    public class Container
    {
        [Fact]
        public void Should_Be_Able_To_Resolve_A_Class_Instance()
        {

            var builder = new ContainerBuilder();
            builder.Register<Cars>();
            var sut = builder.Build();
            Assert.IsNotNull(instance);
        }

    }
}

【问题讨论】:

  • 您是如何创建参考的?你引用的是项目还是编译的DLL?请检查您引用的类中的命名空间。
  • 感谢您提供更多详细信息,命名空间看起来正确。您通过在“添加引用”菜单中选择“项目”添加了引用,对吧?你能单独构建 DI.BLL 项目吗?
  • 旁注:您不需要测试类的任何属性。 xUnit 不使用[TestClass]
  • 其他问题:构建时的所有错误消息到底是什么?有时,某个地方还有另一个错误让编译器感到困惑,它会抱怨无法找到现有的东西。有时,右键单击解决方案并“清理”,然后“重建”可以解除对 Visual Studio 的阻止。
  • Thought [TestClass] 用于所有单元测试。谢谢你让我知道。是的,我可以单独构建它。

标签: c# .net visual-studio .net-core


【解决方案1】:

正如 Geoff James 所说,我使用的是不同版本的框架 项目类使用 Core 框架,而测试项目使用 .NET 4.7。 让它们进入相同的版本,并且运行良好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2021-12-07
    相关资源
    最近更新 更多