Matt
Matt

Reputation: 1616

Why doesn't .NET Core and xUnit recognise my imported namespaces?

I have a project that compiles that I want to test using xUnit. However although the project lets me add the references and builds, as soon as I add a using statement to the test class I get the red squiggly lines and the error "type or namespace could not be found."

Even though the xproj of the project I want to test has been added to references, and the namespaces do exist. It has to be something I have done, but I can't see what. Using earlier versions of .net, I have added references to test projects hundreds of times without an issue.

So what is different in the way .NET works, and why does it not recognize my namespaces in my referenced assemblies?

Update: I have removed xUnit and got MSTest working, but I have the same issue. So this may be a feature of the way I have dotnetcore set up, and the references in my json files more than anything else.

Upvotes: 2

Views: 899

Answers (1)

Nate Barbettini
Nate Barbettini

Reputation: 53640

This structure works for me in .NET Core 1.0:

global.json

{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview2-003121"
  }
}

src/MyLibrary/project.json

{
  "dependencies": { },
  "frameworks": {
    "netstandard1.3": {
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    }
  },
  "version": "1.0.0"
}

test/MyLibrary.Test/project.json

{
  "dependencies": {
    "dotnet-test-xunit": "2.2.0-preview2-build1029",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0"
    },
    "MyLibrary": {
      "target": "project",
      "version": "1.0.0"
    },
    "xunit": "2.1.0"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dnxcore50",
        "portable-net45+win8"
      ]
    }
  },
  "testRunner": "xunit",
  "version": "1.0.0"
}

Using this directory and project structure, both the Visual Studio Test Explorer and dotnet test work and can run xUnit tests.

Upvotes: 1

Related Questions