Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryorah <yoram.harmelin@gmail.com>2012-06-17 02:33:24 +0400
committernulltoken <emeric.fermas@gmail.com>2012-06-19 00:02:31 +0400
commit34a22293c9d36c4cb9721bb408d8dc83b7539c08 (patch)
tree12211673d40049c478cc5746fafc54dec356ff40 /LibGit2Sharp.Tests/MockedRepositoryFixture.cs
parentd62ae53942dc5d06f561f45a0cd957db5809105c (diff)
Show how to test an application using LibGit2Sharp in isolation
Diffstat (limited to 'LibGit2Sharp.Tests/MockedRepositoryFixture.cs')
-rw-r--r--LibGit2Sharp.Tests/MockedRepositoryFixture.cs71
1 files changed, 71 insertions, 0 deletions
diff --git a/LibGit2Sharp.Tests/MockedRepositoryFixture.cs b/LibGit2Sharp.Tests/MockedRepositoryFixture.cs
new file mode 100644
index 00000000..eb552f82
--- /dev/null
+++ b/LibGit2Sharp.Tests/MockedRepositoryFixture.cs
@@ -0,0 +1,71 @@
+using System.Collections.Generic;
+using System;
+using System.Linq;
+using LibGit2Sharp.Tests.TestHelpers;
+using Xunit;
+using Moq;
+
+namespace LibGit2Sharp.Tests
+{
+ // This fixture shows how one can mock the IRepository when writing an application against LibGit2Sharp.
+ // The application we want to test is simulated by the CommitCounter class (see below), which takes an IRepository,
+ // and whose role is to compute and return the number of commits in the given repository.
+ public class MockedRepositoryFixture : BaseFixture
+ {
+ // In this test, we pass to CommitCounter a concrete instance of the Repository. It means we will end up calling the concrete Repository
+ // during the test run.
+ [Fact]
+ public void CanCountCommitsWithConcreteRepository()
+ {
+ using (var repo = new Repository(BareTestRepoPath))
+ {
+ var commitCounter = new CommitCounter(repo);
+ Assert.Equal(7, commitCounter.NumberOfCommits);
+ }
+ }
+
+ // This test shows that CommitCounter can take a mocked instance of IRepository. It means we can test CommitCounter without
+ // relying on the concrete repository. We are testing CommitCounter in isolation.
+ [Fact]
+ public void CanCountCommitsWithMockedRepository()
+ {
+ var commitLog = Mock.Of<CommitLog>(cl => cl.GetEnumerator() == FakeCommitLog(17));
+ var repo = Mock.Of<IRepository>(r => r.Commits == commitLog);
+
+ var commitCounter = new CommitCounter(repo);
+ Assert.Equal(17, commitCounter.NumberOfCommits);
+ }
+
+ private static IEnumerator<Commit> FakeCommitLog(int size)
+ {
+ for (int i = 0; i < size; i++)
+ {
+ yield return FakeCommit(Guid.NewGuid().ToString());
+ }
+ }
+
+ private static Commit FakeCommit(string sha)
+ {
+ var commitMock = new Mock<Commit>();
+ commitMock.SetupGet(x => x.Sha).Returns(sha);
+
+ return commitMock.Object;
+ }
+
+ // Simulated external application ;)
+ private class CommitCounter
+ {
+ private readonly IRepository repo;
+
+ public CommitCounter(IRepository repo)
+ {
+ this.repo = repo;
+ }
+
+ public int NumberOfCommits
+ {
+ get { return repo.Commits.Count(); }
+ }
+ }
+ }
+}