如何从集成测试中使用IConfiguration? [英] How can I use IConfiguration from my integration tests?

查看:231
本文介绍了如何从集成测试中使用IConfiguration?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个API,并且我正在尝试使用XUnit对其进行一些集成测试.这是我的API控制器构造函数:

I have an API, and I'm trying to make some integration tests for it with XUnit. Here's my API controller constructor:

public class MyController : Controller
{
    readonly IMyRepository _myRepository;

    public MyController(IMyRepository myRepository)
    {
        _myRepository = myRepository;
    }

    public async Task<IActionResult> GetUser(Guid userId)
    {
        try
        {
            return Ok(await _my.GetUser(userId));
        }
        catch (Exception ex)
        {
            return StatusCode(500, "An error occurred while handling your request.");
        }
    }
}

我的API控制器正在使用此存储库:

My API controller is using this repository:

public class MyRepository : IMyRepository
{
    private string _connectionString;

    public MyRepository(IConfiguration config)
    {
        _connectionString = config.GetConnectionString("DefaultConnection");
    }


    public async Task<User> GetUser(Guid userId)
    {
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            // call stored proc
        }
    }
}

存储库使用连接字符串在我的方法中进行一些数据库调用.由于配置是在API应用程序的Startup类中设置的,因此当我调用API方法时,此方法有效.

The repository uses the connection string to then make some database calls in my methods. This works when I'm calling my API methods since the configuration was setup in the Startup class of my API application.

但是我不确定如何通过集成测试方法将带有连接字符串的配置对象传递给我的存储库:

But I'm not sure how to pass a configuration object with the connection string to my repository from my integration test methods:

public async Task GetUserShouldReturnOk()
{
    var userId = new Guid();
    var configuration = // ????

    var controller = new MyController(
        new MyRepository(configuration));

    var result = await controller.GetUser(userId);

    Assert.IsType<OkResult>(result);
}

我尝试使用连接字符串信息将json设置文件添加到我的XUnit项目中,然后尝试像Startup类一样构建它,但是出现错误,因为它在测试项目的JSON文件中寻找json文件\ bin \ Debug \ netcoreapp2.0 \目录,所以我也不知道如何将基本路径指向正确的位置:

I've tried adding a json settings file to my XUnit project with my connection string info, and then trying to build it like my Startup class does, but I get an error because it's looking for the json file in my test project's \bin\Debug\netcoreapp2.0\ directory, so I'm not sure how to point the base path to the correct place either:

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json")
    .Build();

有适当的方法吗?

推荐答案

appsettings.json文件仅在我的测试项目根目录中,您知道一种获取当前项目路径的简便方法,这样我就不必对该值进行硬编码

The appsettings.json file is just in my test project root, do you know an easy way to get the current project path so that I don't have to hard-code that value

将文件的Build Action属性设置为Content,以便将其复制到输出目录,以便在测试时将其移动到bin中,然后可以将原始配置代码与.SetBasePath(Directory.GetCurrentDirectory())

Set the Build Action property of the file to Content so it will copy to output directory so it is moved to the bin when testing and then you can use the original config code with the .SetBasePath(Directory.GetCurrentDirectory())

public async Task GetUserShouldReturnOk() {
    var userId = new Guid();
    var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();

    var controller = new MyController(
        new MyRepository(configuration));

    var result = await controller.GetUser(userId);

    Assert.IsType<OkResult>(result);
}

这篇关于如何从集成测试中使用IConfiguration?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆