小编Ant*_*nio的帖子

在AutoFixture自定义上调用Dispose方法

我正在使用AutoFixture自定义来测试访问SQL Compact DB的存储库.

测试完成后立即删除此数据库对我非常有帮助.因为db是在自定义构造函数中创建的,所以我认为删除它的最佳位置是dispose方法.

我想的代码是:

internal class ProjectRepositoryCustomization : ICustomization
{
    private readonly String _dbLocation;

    public ProjectRepositoryCustomization()
    {
        var tempDbLocation = Path.Combine(Path.GetTempPath(), "TempDbToDelete");
        if (!Directory.Exists(tempDbLocation))
        {
            Directory.CreateDirectory(tempDbLocation);
        }

        _dbLocation = Path.Combine(tempDbLocation, Guid.NewGuid().ToString("N") + ".sdf");
    }

    public void Customize(IFixture fixture)
    {   
        DataContextConfiguration.database = _dbLocation;

        var dataContextFactory = new BaseDataContextFactory();
        var projRepository = new ProjectRepository(dataContextFactory);
        fixture.Register(() => projRepository);
    }

    public void Dispose()
    {
        if (File.Exists(_dbLocation))
        {
            File.Delete(_dbLocation);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有可能做类似的事情吗?

c# integration-testing autofixture

7
推荐指数
1
解决办法
467
查看次数

标签 统计

autofixture ×1

c# ×1

integration-testing ×1