Cannot Add Reference To XUnit Test Project

Scottintexas

Well-known member
Joined
Jun 21, 2018
Messages
47
Location
Texas
Programming Experience
5-10
When I add a reference to my Unit Test Project that is referencing my project to be tested, I get errors. I get the little Triangle with the exclamation point in the Solution Explorer and the red squiggly line under any reference to that project in the test code. The path to the project is correct.

This is .Net 6.0...

I don't know where to start fixing this problem. Any help would be appreciated.

Scott
 
Usually test projects are created as standalone libraries. You don't add references to them. You just run the tests straight out of them using the IDE, or point the console test runner to them.
 
Usually test projects are created as standalone libraries. You don't add references to them. You just run the tests straight out of them using the IDE, or point the console test runner to them.
Then how does the test know where the method you want to test is? For example. if I want to write a test to instantiate a new MainWindowViewModel I would write
Instantiate MainWindowViewModel:
        [Fact]
        public void InstantiatesANewMainWindowViewModel()
        {
            MainWindowViewModel mwvm = new MainWinidowViewModel();

        }
Of course, this wont compile because it is not in the same project as the test. I would have to add a reference to the other project or something to tell it where MainWindowViewModel is. Until XUnit I always had to have a reference to the other project. I can't use Unit test because it is for the .Net Framework. So I am trying to learn to use XUnit. So far, I am not doing well.
 
Sorry. I misread your original post. I thought you were trying to to reference your unit test library. Unless you have circular dependency, there should be no issues referencing your assembly to be tested from your unit test assembly.

You just mentioned a red squiggly showing up after adding a reference. What you actually try to build the test assembly, what error(s) show up in the Error pane?
 
The typical "Type or Namespace cannot be found. Are you missing a reference..." error exists when the project is added. The after adding a reference to the SUT project, there is a triangle with exsclamation point in the Test project. To add the reference I go to the project, right click and select Add -> Project Reference. Then I check the checkbox in the list of projects and hit OK. A new object is added to the Solution explorer called "Projects." Expanding the Projects item shows a reference to the project I want to test, with a triangle and exclamation point. Adding a using statement to the test class does not help. Here are a couple of screen shots that may help.
XUnit error.PNG


Using error.PNG
 
Strange. That procedure has always worked worked for me in the past.

Try creating a brand new solution, then create empty SUT library and a test libraries. After that add the project reference to the SUT to the test library. Do you get the same results?
 
This time I have something to work with:
Here is the new solution

Empty Test Solution.PNG


Then I added the Unit test

Added Test Project.PNG



Then I added the project reference.
Added reference.PNG


But this time I have this error to go with it.

Empty Test Error.PNG

It appears I have to edit the csproj file. But I simply chose the .Net 6.0 from the options in both cases when I created the projects.

Scott
 
Add Xunit.core Nuget package last. You want to first make sure that the test assembly can reference the system under test.
 
Open the project properties for the xUnit test project and set Target OS to Windows (under Application, General), it is set to (None) by default.
 
Open the project properties for the xUnit test project and set Target OS to Windows (under Application, General), it is set to (None) by default.
Hi John,

Thanks for the response. It appears to be fixed, but that is on an empty project. I'll start to fill it out now and see what happens. But at least the errors make sense and that problem is solved.
 
Back
Top Bottom