简体   繁体   中英

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. 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. 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. EntityFrameworkRepositoryConfiguration simply contains a connection string, which is passed to BasicRepositoryFactory - which literally just calls return RepositoryFactory.GetInstance<T>(); .

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 .

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; nope. What actually happens is that all changes are committed immediately when you use any CRUD operation on IRepository.

I discovered Batches last night, which are basically delayed queue actions. I should use those for tests, rather than IRepository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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