测试完成后立即删除此数据库对我非常有帮助.因为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); } } }
有可能做类似的事情吗?
解决方法
管理对象的生命周期是您通常希望能够对IoC容器执行的操作.但是,AutoFixture虽然看起来像IoC容器,但它确实是not meant to be one:
AutoFixture shares a lot of similarities with DI Containers. It
supports auto-wiring and it can be configured to create instances in
lots of interesting ways. However,since the focus is different,it
does some things better and some things not as well as a DI Container.
AutoFixture的主要目标是在一些可配置的范围内轻松创建anonymous test data.它的API专注于允许程序员自定义测试数据的生成方式,而不是生成测试数据的时间,因为假设它只被消耗within the context of a test:
AutoFixture is weaker when it comes to lifetime
management. A Fixture is never expected to exist for more than a
single test case,so it makes no sense to model any other lifestyle
than Transient and Singleton. […] It doesn’t have
to,because it’s not a DI Container.
另一方面,测试框架非常擅长管理测试夹具的使用寿命.由于您所描述的内容通常是管理集成测试的上下文的一部分,因此我会在执行夹具中的所有测试之前和之后运行它.
例如:
[TestFixture] public class WithDatabaseContext { private string dbLocation; private BaseDataContextFactory dataContextFactory protected BaseDataContextFactory DataContextFactory { get { return this.dataContextFactory; } } [TestFixtureSetUp] public void FixtureInit() { // Initialize dbLocation // Initialize dataContextFactory } [TestFixtureTearDown] public void FixtureDispose() { // Delete file at dbLocation } }
然后,您的测试可以继承上下文并使用它来配置AutoFixture:
[TestFixture] public void SomeTest : WithDatabaseContext { private IFixture fixture; [SetUp] public void Init() { this.fixture = new Fixture(); this.fixture.Register( () => new ProjectRepository(base.DataContextFactory)); } [Test] public void Doing_something_should_return_something_else() { // ... } }
在这种情况下,利用测试框架来管理临时数据库的生命周期,可以在测试环境中清楚地传达其边界.在我看来,将它隐藏在AutoFixture定制中会使它更不明显,并且可以说更难以使用.