How to pass service config to the Xunit project Te

2019-06-10 21:01发布

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.

0条回答
登录 后发表回答