Spaceman
Spaceman

Reputation: 51

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
    {
    }
}

Upvotes: 2

Views: 1796

Answers (1)

Lews Therin
Lews Therin

Reputation: 3777

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).

Upvotes: 4

Related Questions