0

エンティティ フレームワークと 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>();

私の問題は、追加された要素がまだリポジトリにあるShouldCreateRepositoryAndFindSingleElementBasedOnPredicateために失敗することです-リポジトリが.ShouldCreateRepositoryAndInsertIntoItWithoutExceptionsCleanup

この問題を解決するにはどうすればよいですか?

4

1 に答える 1