【问题标题】:The type or namespace name 'Startup' could not be found找不到类型或命名空间名称“Startup”
【发布时间】:2016-11-29 01:27:11
【问题描述】:

我正在尝试向我的项目添加集成测试,但在我的 Tests.cs 文件中不断收到错误消息“找不到类型或命名空间名称‘Startup’”。

我有两个 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"}}

Test 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 类如下所示:

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();
}
}}

有人知道我为什么会收到这个错误吗?提前致谢!

【问题讨论】:

  • 您的Startup 类在哪里定义?您可能需要添加一个 using 指令来引用您的命名空间或使用命名空间 Your.Namespace.Startup 的完整类名。

标签: asp.net-core integration-testing xunit


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

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

    using [namespace the Startup class is in];
    

    应该就是这样。

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

    【讨论】:

      【解决方案3】:

      你能补充一下吗

      使用控制台应用程序;

      在程序的顶部。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-12
        • 2011-05-13
        • 2013-03-25
        • 2012-06-19
        • 2017-11-29
        • 2012-09-27
        • 2011-05-06
        相关资源
        最近更新 更多