c# - NUnit 测试无法识别我创建的类 - "The type or namespace name could not be found"

标签 c# visual-studio nunit-3.0

我正在设置基本的 NUnit 测试项目来测试 STACK 对象。我创建了 MyStack 类和 TestClassTestClass 找不到对我创建的 MyStack 类的引用。

The type or namespace name 'MyStack' could not be found (are you missing a using directive or an assembly reference?

我正在使用 NUnit 和 NUnit3TestAdapter。我已将包含 MyStack 的项目添加到测试项目的引用中。

测试类

using NUnit.Framework;

namespace NUnit.TDDStackTests
{
    [TestFixture]
    public class TestClass
    {
        [Test]
        public void TestMethod()
        {
            MyStack stack = new MyStack();
        }
    }
}

我的堆栈

namespace TDDStack
{
    class MyStack
    {
    }
}

最佳答案

我发现两个可能的问题。首先,MyStack 类是私有(private)。如果 class 关键字之前没有其他修饰符,C# 将非嵌套类型默认为 private,并将嵌套类型默认为 internal

尝试将 public 关键字添加到您的 MyStack 类定义中:

public class MyStack

其次,MyStack 位于 TDDStack 命名空间中,但您尝试在 NUnit.TDDStackTests 的类中创建它的实例> 命名空间。要解决此问题,您可以在单元测试中为命名空间添加 using 语句:

using NUnit.Framework;
using TDDStack; // Add this

namespace NUnit.TDDStackTests
{
    [TestFixture]
    public class TestClass

    // etc ...

或者您可以在 MyStack 的每次使用前加上包含它的命名空间:

var stack = new TDDStack.MyStack();

如果这些类位于单独的项目中,您还需要在要使用它的项目(单元测试项目)中添加对包含 MyStack 的项目的引用。

关于c# - NUnit 测试无法识别我创建的类 - "The type or namespace name could not be found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56156013/

相关文章:

c# - mvc脚手架从数据库中删除原始数据

c# - HttpWebRequest 是否自动发送 200 OK?

visual-studio - SDK风格的csproj项目中的 "none include"和 "none update"有什么区别?

c++ - 从 x32 迁移到 x64 时出现链接器错误

c# - 如何通过异步方法使用 Assert.Catch

c# - 如果在 'Passed' 阶段失败,为什么测试标记为 "pre-test"?

c# - Structuremap 是否支持开箱即用的 Lazy?

c# - "System is undefined"使用点击处理程序时出现 JavaScript 错误?

c++ - cocos2dx最新版本如何设置标题

c# - 使用 Nunit 3.x 的参数化测试