我正在嘗試爲我的應用程序使用實體框架和sharprepository編寫集成測試。我正在編寫一些測試,我注意到當我在TestCleanup期間調用Dispose()時,我在測試中添加到存儲庫的數據不會被刪除。我的代碼如下:SharpRepository未在測試中處置存儲庫?
[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實現,而是要確保我已經正確配置實體框架。 EntityFrameworkRepositoryConfiguration
只是包含一個連接字符串,該字符串被傳遞給BasicRepositoryFactory
- 它實際上只是調用return RepositoryFactory.GetInstance<T>();
。
我的問題是,ShouldCreateRepositoryAndFindSingleElementBasedOnPredicate
失敗,因爲在ShouldCreateRepositoryAndInsertIntoItWithoutExceptions
中添加的元素仍在存儲庫中 - 即使存儲庫應該已在Cleanup
中處理。
我該怎麼辦才能解決這個問題?
當你說在第一個測試中添加的元素仍然在版本庫中時,你究竟是什麼意思?什麼是EF知識庫指向?我可能會感到困惑,但是當你添加數據時,數據將被添加到連接字符串指向的數據庫中,所以它將會在那裏,除非你做一些刪除調用來刪除它。我可能會錯過某些東西或誤解了這個問題。 –
我很蠢。我認爲Repository的工作方式與EF知識庫相似(a la,除非提交,否則不承諾更改);它不,我昨晚發現了Batches。 –