简体   繁体   English

SharpRepository是否在测试中不处置存储库?

[英]SharpRepository is not disposing of repositories in testing?

I'm trying to write an integration test for my application that utilizes entity framework and sharprepository. 我正在尝试为利用实体框架和SharpRepository的应用程序编写集成测试。 I'm writing some tests at the moment and I've noticed that data that I add to the repository in the tests is not being removed when I call Dispose() during TestCleanup. 目前,我正在编写一些测试,我注意到在TestCleanup期间调用Dispose()时,不会删除添加到测试中存储库中的数据。 My code is as follows: 我的代码如下:

    [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);
    }

The main purpose of these tests is not to test the SharpRepository implementation of EntityFramework, but rather to make sure that I've configured Entity Framework correctly. 这些测试的主要目的不是测试EntityFramework的SharpRepository实现,而是确保我已经正确配置了Entity Framework。 EntityFrameworkRepositoryConfiguration simply contains a connection string, which is passed to BasicRepositoryFactory - which literally just calls return RepositoryFactory.GetInstance<T>(); EntityFrameworkRepositoryConfiguration只是包含一个连接字符串,该字符串将传递给BasicRepositoryFactory ,它只是调用return RepositoryFactory.GetInstance<T>(); BasicRepositoryFactory .

My issue is is that ShouldCreateRepositoryAndFindSingleElementBasedOnPredicate fails because the element added in ShouldCreateRepositoryAndInsertIntoItWithoutExceptions is still in the repository - even though the repository should have been disposed in Cleanup . 我的问题是, ShouldCreateRepositoryAndFindSingleElementBasedOnPredicate失败,因为在ShouldCreateRepositoryAndInsertIntoItWithoutExceptions添加的元素仍在存储库中-即使该存储库应已在Cleanup进行了处理。

What can I do to fix this issue? 我该怎么做才能解决此问题?

I was stupid - I automatically assumed that IRepository would work like the Entity Framework repository in that all actions are interally queued until you call SaveChanges; 我很傻-我自动假定IRepository会像Entity Framework存储库那样工作,因为所有操作都在队列中排队,直到您调用SaveChanges为止。 nope. 不。 What actually happens is that all changes are committed immediately when you use any CRUD operation on IRepository. 实际上发生的是,当您在IRepository上使用任何CRUD操作时,所有更改都将立即提交。

I discovered Batches last night, which are basically delayed queue actions. 我昨晚发现了批处理,这基本上是延迟的队列操作。 I should use those for tests, rather than IRepository. 我应该将其用于测试,而不是IRepository。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM