Commit 6a754a74 authored by Scott DePouw's avatar Scott DePouw

ApiToDoItemsControllerListShould now uses CustomWebApplicationFactory.

parent 6e652f69
using CleanArchitecture.Core.Entities; using CleanArchitecture.Core.Entities;
using CleanArchitecture.Web; using CleanArchitecture.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks; using System.Threading.Tasks;
using Xunit; using Xunit;
namespace CleanArchitecture.Tests.Integration.Web namespace CleanArchitecture.Tests.Integration.Web
{ {
public class ApiToDoItemsControllerList : BaseWebTest public class ApiToDoItemsControllerList : IClassFixture<CustomWebApplicationFactory<Startup>>
{ {
public HttpClient Client { get; }
public ApiToDoItemsControllerList(CustomWebApplicationFactory<Startup> factory)
{
Client = factory.CreateClient();
}
[Fact] [Fact]
public async Task ReturnsTwoItems() public async Task ReturnsTwoItems()
{ {
var response = await _client.GetAsync("/api/todoitems"); var response = await Client.GetAsync("/api/todoitems");
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync(); var stringResponse = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<IEnumerable<ToDoItem>>(stringResponse).ToList(); var result = JsonConvert.DeserializeObject<IEnumerable<ToDoItem>>(stringResponse).ToList();
......
using CleanArchitecture.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.ViewComponents;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using System.Net.Http;
using System.Reflection;
namespace CleanArchitecture.Tests.Integration.Web
{
public abstract class BaseWebTest
{
protected readonly HttpClient _client;
public BaseWebTest()
{
_client = GetClient();
}
protected HttpClient GetClient()
{
var startupAssembly = typeof(Startup).GetTypeInfo().Assembly;
var contentRoot = GetProjectPath("src", startupAssembly);
var builder = new WebHostBuilder()
.UseContentRoot(contentRoot)
.ConfigureServices(InitializeServices)
.UseStartup<Startup>()
.UseEnvironment("Testing"); // ensure ConfigureTesting is called in Startup
var server = new TestServer(builder);
var client = server.CreateClient();
return client;
}
protected virtual void InitializeServices(IServiceCollection services)
{
var startupAssembly = typeof(Startup).GetTypeInfo().Assembly;
// Inject a custom application part manager. Overrides AddMvcCore() because that uses TryAdd().
var manager = new ApplicationPartManager();
manager.ApplicationParts.Add(new AssemblyPart(startupAssembly));
manager.FeatureProviders.Add(new ControllerFeatureProvider());
manager.FeatureProviders.Add(new ViewComponentFeatureProvider());
services.AddSingleton(manager);
}
/// <summary>
/// Gets the full path to the target project path that we wish to test
/// </summary>
/// <param name="solutionRelativePath">
/// The parent directory of the target project.
/// e.g. src, samples, test, or test/Websites
/// </param>
/// <param name="startupAssembly">The target project's assembly.</param>
/// <returns>The full path to the target project.</returns>
private static string GetProjectPath(string solutionRelativePath, Assembly startupAssembly)
{
// Get name of the target project which we want to test
var projectName = startupAssembly.GetName().Name;
// Get currently executing test project path
// AppContext.BaseDirectory or AppDomain.CurrentDomain.BaseDirectory contain the BaseDirectory.
var applicationBasePath = AppContext.BaseDirectory;
// Find the folder which contains the solution file. We then use this information to find the target
// project which we want to test.
var directoryInfo = new DirectoryInfo(applicationBasePath);
do
{
var solutionFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, "CleanArchitecture.sln"));
if (solutionFileInfo.Exists)
{
return Path.GetFullPath(Path.Combine(directoryInfo.FullName, solutionRelativePath, projectName));
}
directoryInfo = directoryInfo.Parent;
}
while (directoryInfo.Parent != null);
throw new Exception($"Solution root could not be located using application root {applicationBasePath}.");
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment