14

In my namespace Draughts I have two projects, Draughts and Draughts.UnitTests. When I try to access Draughts methods/classes in Draughts.UnitTests it can't find anything at all. At the top of Draughts.UnitTests I put using Draughts. Any ideas?

BoardUnitTests.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Draughts;
using NUnit.Framework;

namespace Draughts.UnitTests
{
    public class BoardUnitTests
    {
        private Board GetBoard()
        {
            return true;
        }

        [Test]
        public void CheckValidBoardPosition_ValidPosition_ReturnsTrue()
        {
            Assert.AreEqual(1, 1);
        }
    }
}

In the code above it can't recognize Board which is a class I have defined in Draughts.

Here's a screenshot of my solution explorer:

enter image description here

Bassie
  • 9,529
  • 8
  • 68
  • 159
Dockson
  • 542
  • 2
  • 6
  • 16

4 Answers4

24

Probably this is related to one of these things

  1. You don't have a reference to Draughts on your unit test project. Right click on the test project, then Add > Reference and select the project being tested.

  2. Classes on Draughts are not public so you can't see them outside the project they belongs to

palako
  • 3,342
  • 2
  • 23
  • 33
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • 4
    Did not know you had to explicitly add a reference through Add > Reference in solution explorer. Thanks. – Dockson Nov 09 '16 at 20:36
  • #1 here will be the answer 9 out of 10 times. Note that you can break an existing project reference simply by removing the project temporarily and then adding it back in. I highly recommend people read this to know what is going on behind the scenes: https://learn.microsoft.com/en-us/visualstudio/ide/managing-references-in-a-project?view=vs-2022#project-to-project-references – Cale Sweeney Nov 29 '22 at 14:19
  • Also make sure to compile the test project after adding the reference – joekevinrayan96 Dec 15 '22 at 00:37
11

I realize this is pretty old, but I came across this question while looking for a solution to my issue. For me it turned out that my Test project and the Tested project were on two different versions of the .NET Framework. I updated one, but forgot the other. Once I updated the test project to match the tested, everything worked like it should

Hope this helps the next person! :)

Rick Runowski
  • 346
  • 3
  • 9
1

I ran into a similar issue as I used dotnet cli to create a test project and .net core project. I forgot to add the test project to my solution (right click solution > add > existing project). Once I did that, everything worked. Not the answer for OP but leaving it here in hopes it might help other people in future.

Leslie Alldridge
  • 1,427
  • 1
  • 10
  • 26
0

In my case it was because I had them compiling on different versions of .net

JohnFF
  • 723
  • 5
  • 20