[英]SharpRepository is not disposing of repositories in testing?
我正在尝试为利用实体框架和SharpRepository的应用程序编写集成测试。 目前,我正在编写一些测试,我注意到在TestCleanup期间调用Dispose()时,不会删除添加到测试中存储库中的数据。 我的代码如下:
[TestInitialize]
public void Initialize()
{
var config = new EntityFrameworkRepositoryConfiguration(null);
_factory = new BasicRepositoryFactory(config);
_channelRepository = _factory.CreateRepository<Channel>();
}
[TestCleanup]
public void Cleanup()
{
_channelRepository.Dispose();
}
[TestMethod]
public void ShouldCreateRepositoryAndExecuteGetAllWithoutExceptions()
{
_channelRepository.GetAll();
}
[TestMethod]
public void ShouldCreateRepositoryAndInsertIntoItWithoutExceptions()
{
var repo = _factory.CreateRepository<Channel>();
// NB: We can't mock this. Has to be real stuff.
var channel = new Channel() { Id = 1 };
_channelRepository.Add(channel);
Assert.AreSame(channel, _channelRepository.Get(1));
}
[TestMethod]
public void ShouldCreateRepositoryAndFindSingleElementBasedOnPredicate()
{
var channels = new[]
{
new Channel(),
new Channel(),
new Channel()
};
_channelRepository.Add(channels);
var firstOfPredicate = _channelRepository.Find(x => x.Id > 3);
Assert.IsTrue(_channelRepository.Count() == channels.Length,
"Seeded array has {0} elements, but the repository has {1}.",
channels.Length,
_channelRepository.Count());
Assert.AreEqual(channels[2].Id, firstOfPredicate.Id);
}
这些测试的主要目的不是测试EntityFramework的SharpRepository实现,而是确保我已经正确配置了Entity Framework。 EntityFrameworkRepositoryConfiguration
只是包含一个连接字符串,该字符串将传递给BasicRepositoryFactory
,它只是调用return RepositoryFactory.GetInstance<T>();
BasicRepositoryFactory
。
我的问题是, ShouldCreateRepositoryAndFindSingleElementBasedOnPredicate
失败,因为在ShouldCreateRepositoryAndInsertIntoItWithoutExceptions
添加的元素仍在存储库中-即使该存储库应已在Cleanup
进行了处理。
我该怎么做才能解决此问题?
我很傻-我自动假定IRepository会像Entity Framework存储库那样工作,因为所有操作都在队列中排队,直到您调用SaveChanges为止。 不。 实际上发生的是,当您在IRepository上使用任何CRUD操作时,所有更改都将立即提交。
我昨晚发现了批处理,这基本上是延迟的队列操作。 我应该将其用于测试,而不是IRepository。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.