找不到类型或命名空间名称 'Startup'

The type or namespace name 'Startup' could not be found

我正在尝试向我的项目添加集成测试,但我的 Tests.cs 文件中一直出现错误 "The type or namespace name 'Startup' cound not be found"。

我有两个 project.json 文件,一个在我的 src 项目中,另一个在我的测试项目中。

Src project.json 看起来像这样:

{
"dependencies": {
"Microsoft.NETCore.App": {
  "version": "1.0.1",
  "type": "platform"
},
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
"Microsoft.EntityFrameworkCore.Tools": {
  "version": "1.0.0-preview2-final",
  "type": "build"
},
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.AspNetCore.StaticFiles": "1.0.0-*",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.2",
"Swashbuckle": "6.0.0-beta902" },
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
// "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview2-final",
"Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final"},
"frameworks": {
"netcoreapp1.0": {
  "imports": [
    "dotnet5.6",
    "portable-net45+win8"]}},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"debugType": "portable",
"xmlDoc": true},
"runtimeOptions": {
"configProperties": {
  "System.GC.Server": true
}},
"publishOptions": {
"include": [
  "wwwroot",
  "**/*.cshtml",
  "appsettings.json",
  "web.config",
  "Dockerfile.debug",
  "Dockerfile",
  "docker-compose.debug.yml",
  "docker-compose.yml"
]},
"scripts": {
"postpublish": [
  "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%"
]},
"tooling": {
 "defaultNamespace": "api"}}

测试 project.json 看起来像这样:

{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable"},
"dependencies": {
"System.Runtime.Serialization.Primitives": "4.1.1",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-rc2-*",
"SrcService": {
  "target": "project"
},
"Microsoft.AspNetCore.TestHost": "1.0.0"},
"testRunner": "xunit",
"frameworks": {
"netcoreapp1.0": {
  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.1"
    }
  },
  "imports": [
    "dotnet5.4",
    "portable-net45+win8"
  ]
}}}

我的 Tests.cs class 看起来像这样:

using System;
using Xunit;
using Microsoft.AspNetCore.TestHost;
using System.Net.Http;
using Microsoft.AspNetCore.Hosting;


namespace Tests
{
public class Tests
{
    public TestServer server { get; }
    public HttpClient client { get; }

    public Tests(){
        var builder = new WebHostBuilder().UseStartup<Startup>();
        server = new TestServer(builder);
        client = server.CreateClient();
    }

[Fact]
public async void TestVisitRoot() {
    var response = await client.GetAsync("/");
    response.EnsureSuccessStatusCode();
}
}}

有谁知道我为什么会收到此错误?提前致谢!

我在你的测试项目Project.json中看到

"SrcService": {
   "target": "project"
}

所以这意味着您的 Startup class 在此项目中。 为了在您的 Test.cs 中被识别,您只需添加 using

using [namespace the Startup class is in];

应该就是这个了。

提示:如果您正在使用 Visual Studio,当您遇到无法识别的内容时。只需单击它,然后按 CTRL + .(或右键单击并使用 VS2015 Quick action and refactoring

你能加吗

using ConsoleApplication;

在程序的顶部。

检查 Startup.csnamespace 它应该与 Program.cs 中的相同。

Program.cs

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;


namespace dotnetcoretester  // <------- This
{
    class Program
    {
     ...

Startup.cs

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace dotnetcoretester // <------ and This
{
  public class Startup
  {
    public void Configure(IApplicationBuilder app)
    ...