AppSettings.json for Integration Test in ASP.NET Core

后端 未结 3 1128
花落未央
花落未央 2020-12-10 01:46

I am following this guide. I have a Startup in the API project that uses an appsettings.json configuration file.

public class Startu         


        
相关标签:
3条回答
  • 2020-12-10 02:22

    In the end, I followed this guide, specifically the Integration Testing section towards the bottom of the page. This removes the need to copy the appsettings.json file to the output directory. Instead it tells the test project the actual directory of the web application.

    As for copying the appsettings.json to the output directory, I also managed to get that to work. Combined with the answer from dudu, I used include instead of includeFiles so the resulting section would look something like this:

    "buildOptions": {
        "copyToOutput": {
            "include": "appsettings.json"
        }
    }
    

    I'm not entirely sure why this works but it does. I had a quick look at the documentation but couldn't find any real differences and since the original problem was essentially solved I didn't look further.

    0 讨论(0)
  • 2020-12-10 02:25

    Remove "emitEntryPoint": true in test project.json file.

    0 讨论(0)
  • 2020-12-10 02:34

    Integration test on ASP.NET.Core 2.0 follow MS guide ,

    You should right click appsettings.json set its property Copy to Output directory to Copy always

    And now you could find the json file in output folder, and build TestServer with

    var projectDir = GetProjectPath("", typeof(TStartup).GetTypeInfo().Assembly);
    _server = new TestServer(new WebHostBuilder()
        .UseEnvironment("Development")
        .UseContentRoot(projectDir)
        .UseConfiguration(new ConfigurationBuilder()
            .SetBasePath(projectDir)
            .AddJsonFile("appsettings.json")
            .Build()
        )
        .UseStartup<TestStartup>());
    
    
    
    /// Ref: https://stackoverflow.com/a/52136848/3634867
    /// <summary>
    /// Gets the full path to the target project that we wish to test
    /// </summary>
    /// <param name="projectRelativePath">
    /// 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 projectRelativePath, 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 = System.AppContext.BaseDirectory;
    
        // Find the path to the target project
        var directoryInfo = new DirectoryInfo(applicationBasePath);
        do
        {
            directoryInfo = directoryInfo.Parent;
    
            var projectDirectoryInfo = new DirectoryInfo(Path.Combine(directoryInfo.FullName, projectRelativePath));
            if (projectDirectoryInfo.Exists)
            {
                var projectFileInfo = new FileInfo(Path.Combine(projectDirectoryInfo.FullName, projectName, $"{projectName}.csproj"));
                if (projectFileInfo.Exists)
                {
                    return Path.Combine(projectDirectoryInfo.FullName, projectName);
                }
            }
        }
        while (directoryInfo.Parent != null);
    
        throw new Exception($"Project root could not be located using the application root {applicationBasePath}.");
    }
    

    Ref: TestServer w/ WebHostBuilder doesn't read appsettings.json on ASP.NET Core 2.0, but it worked on 1.1

    0 讨论(0)
提交回复
热议问题