我正在尝试创建一个集成测试项目,用于测试我的web api .Net Core以及此处的Microsoft示例最佳实践https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing但是我没有_1083051找到'testServer' . 而我'm sure my '路线' is right [ 1083050 + guid] , if i try on debug mode from iisexpress. it'的工作很棒
如果有人有一些想法来解决它,将非常感激 . 谢谢
这是我在Integration Test项目中使用的代码:
RegistrationIntegrationTest.cs:
[TestClass]
public class RegistrationIntegrationTest
{
private static TestServer server;
private static HttpClient client;
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var projectPath = Path.GetFullPath(Path.Combine(basePath, "../../../../Registration"));
var builder = new WebHostBuilder()
.UseContentRoot(projectPath)
.UseEnvironment(EnvironmentName.Development)
.UseStartup<Startup>();
server = new TestServer(builder);
client = server.CreateClient();
}
private async Task<string> GetRegistrationResponse(string guid)
{
var request = "api/Registration/" + guid;
var response = await client.GetAsync(request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
[TestMethod]
public async Task GetRegistration()
{
// Act
var responseString = await GetRegistrationResponse("A5A3CBFD-8B61-E711-80E2-00505693113A");
// Assert
Assert.Equals("test",
responseString);
}
}
这是我的Integration Test项目中的启动类:
Startup.cs
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
//.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
//.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddCors();
services.AddAntiforgery();
services.AddSingleton<IControllerActivator>(new SimpleInjectorControllerActivator(this.Container));
services.Configure<MyAppSettings>(Configuration);
services.UseSimpleInjectorAspNetRequestScoping(this.Container);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
if (env.IsDevelopment())
{
loggerFactory.AddDebug();
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
}
else if (env.IsStaging())
{
app.UseCors(builder => builder.WithOrigins("https://apitest.swisscaution.ch"));
}
else if (env.IsProduction())
{
app.UseCors(builder => builder.WithOrigins("https://internalapi.swisscaution.ch"));
}
else
{
throw new InvalidOperationException("Bad application environement.");
}
app.UseMvc();
this.InitializeContainer(app);
}
这是我的解决方案Visual Studio架构的照片,正如您所看到的,集成测试项目与我的web api项目分开,名为'registration'
我得到的例外:
1 回答
我终于找到了问题 . 是因为我的[集成测试项目]是.Net核心版本的目标,我试图用.Net Framework 4.5.6版本测试和托管项目 . 我在线找到解决方案,我需要修改csproj文件并在“PropertyGroup”部分添加这个3配置 .
这是我找到的网址:http://quabr.com/44027215/system-net-http-httpclient-microsoft-aspnetcore-testhost-testserver-createclient