如何将服务配置传递给Xunit项目测试控制器?

时间:2017-12-21 16:08:06

标签: c# asp.net-core xunit fixtures xunit.net

目前我的设置如下。

启动类: 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 ...
}

类: 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; } 
}

接口类: IAzureService

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

Azure类: AzureService ,由上面的界面

使用
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)
    {
        //...
    }    
}

图像控制器: 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...         
    }
}

使用xunit

在下面的类(TEST Library)中的主要问题

分类: 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..
}

如何将注入Xunit ImageControllerTest构造函数。

  • storageConfig 类对象和
  • IAzureService 对象 从Xunit ImageControllerTest方法进入ImageController。?

如果您有任何想法或解决方案,请与我分享。

0 个答案:

没有答案