簡體   English   中英

SharpRepository是否在測試中不處置存儲庫?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM