0

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?

4

1 回答 1

0

我很愚蠢——我自动假设 IRepository 会像实体框架存储库一样工作,因为所有操作都在内部排队,直到您调用 SaveChanges;不。实际发生的是,当您在 IRepository 上使用任何 CRUD 操作时,所有更改都会立即提交。

我昨晚发现了批处理,这基本上是延迟的队列操作。我应该将它们用于测试,而不是 IRepository。

于 2014-01-15T08:27:59.147 回答