我试图将集成测试添加到我的项目中,但我一直收到错误"类型或命名空间名称' Startup'找不到"在我的Tests.cs文件中。
我有两个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"
]
}}}
My 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();
}
}}
有谁知道我为什么会收到此错误?提前谢谢!
答案 0 :(得分:3)
检查您namespace
中的Startup.cs
,该地址应与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)
...
答案 1 :(得分:1)
我在您的测试项目Project.json
中看到了
"SrcService": {
"target": "project"
}
这意味着你的Startup类就在这个项目中。
要在Test.cs中识别,您只需添加using
using [namespace the Startup class is in];
那应该是这个。
提示:如果您使用的是Visual Studio,那么当您遇到类似这样的问题时。只需点击它,然后按CTRL + .
(或右键单击Quick action and refactoring
与VS2015)
答案 2 :(得分:0)
您可以添加
程序顶部的使用ConsoleApplication;
。