GetContent root Web API .NET Core xunit ClassFixture

GetContent root Web API .NET Core xunit ClassFixture

我正在尝试构建一个将驻留在其自己的 DLL 中的基本装置,以便它可以被许多测试项目使用。

我在设置内容根时遇到问题

.UseContentRoot(projectPath)

下面的内容有效,但我正在硬编码 solutionName

问题

如何在不硬编码的情况下获取 contentRoot?

我可以在我的 baseFixture 中注入任何东西吗? IHostingEnvironmentsolutionName?

public class BaseFixture<TStartup> : IDisposable where TStartup : class
{

    public BaseFixture()
    {
        var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;

        var projectPath = GetProjectPath(startupAssembly);
        var host = new WebHostBuilder()
                   .UseContentRoot(projectPath)
                   .UseStartup(typeof(TStartup));

        Server = new TestServer(host);
        Client = Server.CreateClient();


    }


    private  string GetProjectPath(Assembly startupAssembly)
    {
        //Get name of the target project which we want to test
        var projectName = startupAssembly.GetName().Name;

        //Get currently executing test project path
        var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath;

        //Find the folder which contains the solution file. We then use this information to find the 
        //target project which we want to test
        DirectoryInfo directoryInfo = new DirectoryInfo(applicationBasePath);
        do
        {
            var solutionFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, "HardCodedSolutionName.sln"));
            if (solutionFileInfo.Exists)
            {
                return Path.GetFullPath(Path.Combine(directoryInfo.FullName, projectName));
            }
            directoryInfo = directoryInfo.Parent;
        }
        while (directoryInfo.Parent != null);

        throw new Exception($"Solution root could not be located using application root {applicationBasePath}");

    }

    public TestServer Server { get; set; }
    public HttpClient Client { get; }


    public void Dispose()
    {
        Server.Dispose();
        Client.Dispose();
    }
}

为什么不在目录树中向上搜索 *.sln 文件以获取解决方案名称?

private string GetProjectPath(Assembly startupAssembly) {
    //Get name of the target project which we want to test
    var projectName = startupAssembly.GetName().Name;

    //Get currently executing test project path
    var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath;

    //Find the folder which contains the solution file. 
    //We then use this information to find the target project which we want to test
    DirectoryInfo directoryInfo = new DirectoryInfo(applicationBasePath);
    do {
        //find *.sln files
        var solutionFileInfo = directoryInfo.GetFiles("*.sln").FirstOrDefault();
        if (solutionFileInfo != null) {
            return Path.GetFullPath(Path.Combine(directoryInfo.FullName, projectName));
        }                
        directoryInfo = directoryInfo.Parent;
    }
    while (directoryInfo.Parent != null);

    throw new Exception($"Solution root could not be located using application root {applicationBasePath}");

}