简体   繁体   中英

NUnit test does not recognize a class that I created - “The type or namespace name could not be found”

I'm setting up basic NUnit test project to test a STACK object. I have created the MyStack class and a TestClass . The TestClass cannot find reference to the MyStack class that I created.

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

I'm using NUnit & NUnit3TestAdapter. I have added the project containing MyStack to the References of the test project.

TestClass

using NUnit.Framework;

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

MyStack

namespace TDDStack
{
    class MyStack
    {
    }
}

I see two possible issues. The first is that the MyStack class is private . C# defaults non-nested types to private if there is no other modifier before the class keyword, and nested types to internal .

Try adding the public keyword to your MyStack class definition:

public class MyStack

Second, MyStack is in the TDDStack namespace, but you are trying to create an instance of it in a class in the NUnit.TDDStackTests namespace. To fix this, you can either add a using statement for the namespace in the unit test:

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

namespace NUnit.TDDStackTests
{
    [TestFixture]
    public class TestClass

    // etc ...

Or you can just prefix every usage of MyStack with the namespace that contains it:

var stack = new TDDStack.MyStack();

If the classes are in separate projects, you will also need to add a reference to the project containing MyStack in the project where you want to use it (the unit test project).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM