How to pass service config to the Xunit project Test controller?

心不动则不痛 提交于 2019-12-13 03:33:08

问题


I have below setup currently.

Startup Class : Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<AzureStorageConfig>(Configuration.GetSection("AzureStorageConfig"));
        services.AddTransient<IAzureService, AzureService>();
    }
    //other config settings ...
}

Class: AzureStorageConfig

//store the azure account details etc...
public class AzureStorageConfig
{ 
    public string AzureURL { get; set; } 
    public string StorageConnectionString { get; set; } 
    public string AccountName { get; set; } 
}

Interface class: IAzureService

public interface IAzureService
{
    Task<string> UploadFileAsync(AzureStorageConfig _storageConfig, string filename);
    Task<string> DeleteFile(AzureStorageConfig _storageConfig, string filename);
}

Azure class : AzureService that use by interface above

public class AzureService : IAzureService, IDisposable
{
    // Here implemented above service methods..
    public Task<string> UploadFileAsync(AzureStorageConfig _storageConfig, string filename)
    {
        //...
    }

    Task<string> DeleteFile(AzureStorageConfig _storageConfig, string filename)
    {
        //...
    }    
}

Image controller: ImageController.cs

[Produces("application/json")]
[Route("api/Images")]
public class ImagesController : Controller
{
    #region Private properties
    private readonly ApiDbContext _context;
    private readonly IMapper _mapper;
    private AzureStorageConfig _storageConfig;
    public readonly IAzureService _azureService;
    #endregion

    #region Constructor

    public ImagesController(ApiDbContext context, IMapper mapper, IOptions<AzureStorageConfig> storageConfig, IAzureService azureService)
    {
        _context = context;
        _mapper = mapper;
        _storageConfig =  storageConfig.Value;
        _azureService = azureService;
    }

    // other POST and Delete methods written
    public async Task<IActionResult> PostImage(Guid Id, [FromForm] ICollection<IFormFile> files)
    {
        // here used   _storageConfig objects to use account key and names...         
    }
}

Main issue in below class ( TEST Library ) with xunit

Class: ImageControllerTest

[Collection("TestDb")] 
public class ImageControllerTest : IClassFixture<InitializeAutoMap>
{
    private ImagesController _controller;
    private DatabaseFixture _fixture; 
    private InitializeAutoMap _initialize;

    public ImageControllerTest(DatabaseFixture fixture, InitializeAutoMap initialize)
    {
        this._fixture = fixture; 
        this._initialize = initialize;

        // How to pass service object and StorageConfig to the main 
        //ImageController from this TestController for testing.
        _controller = new ImagesController(_context, 
            _initialize.InstanceMapper,
            /*<here I want to pass storageConfig object>*/,
            /*<here I want to pass service object>*/
        ); 
    }

    // other [Fact] testing done.
    // other codes..
}

How to it will possible to get injected to Xunit ImageControllerTest constructor.

  • storageConfig class object and
  • IAzureService object into the ImageController from Xunit ImageControllerTest method.?

Share me if you have idea or solutions.

来源:https://stackoverflow.com/questions/47928678/how-to-pass-service-config-to-the-xunit-project-test-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!