Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

c# - How can I use IConfiguration from my integration tests?

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.");
        }
    }
}

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
        }
    }
}

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);
}

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 inDebug etcoreapp2.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();

Is there a proper way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...