Reputation: 303
I'm trying to add integration testing to my project but I keep getting the error "The type or namespace name 'Startup' cound not be found" in my Tests.cs file.
I have two project.json files, one in my src project and the other in my test project.
Src project.json looks like this:
{
"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"}}
The Test project.json looks like this:
{
"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 class looks like this:
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();
}
}}
Does anyone know why I'm getting this error? Thanks in advance!
Upvotes: 9
Views: 9986
Reputation: 5715
Check namespace
of your Startup.cs
it should be the same as in 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)
...
Upvotes: 11
Reputation: 99
Can you add
using ConsoleApplication;
at the top of the program.
Upvotes: 0
Reputation: 7804
I see in your Project.json
in your test project
"SrcService": {
"target": "project"
}
So it means your Startup class is in this project.
In order to be recognized in your Test.cs you just need to add the using
using [namespace the Startup class is in];
And that should be just this.
Hint: if you are using Visual Studio, when you have something not recognized like this. Just click on it and then press CTRL + .
(or right click and Quick action and refactoring
with VS2015)
Upvotes: 4