diff options
author | Keith Dahlby <dahlbyk@gmail.com> | 2013-11-21 01:52:21 +0400 |
---|---|---|
committer | Keith Dahlby <dahlbyk@gmail.com> | 2013-11-25 02:32:46 +0400 |
commit | b8dc66023e3b735d409bef04eee531342161dad2 (patch) | |
tree | 6f369d9389317f873436621e82ea757ca2d1dc25 /LibGit2Sharp.Tests | |
parent | e3108d4153590dcbe384bf88d1e357cace2648c7 (diff) |
Configuration.Testability++
Diffstat (limited to 'LibGit2Sharp.Tests')
-rw-r--r-- | LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj | 2 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/MockingFixture.cs (renamed from LibGit2Sharp.Tests/MockedRepositoryFixture.cs) | 46 |
2 files changed, 43 insertions, 5 deletions
diff --git a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj index 9a6c57a3..ab526ec1 100644 --- a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj +++ b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj @@ -80,7 +80,7 @@ <Compile Include="CleanFixture.cs" /> <Compile Include="CurrentOperationFixture.cs" /> <Compile Include="MetaFixture.cs" /> - <Compile Include="MockedRepositoryFixture.cs" /> + <Compile Include="MockingFixture.cs" /> <Compile Include="ConfigurationFixture.cs" /> <Compile Include="AttributesFixture.cs" /> <Compile Include="CommitAncestorFixture.cs" /> diff --git a/LibGit2Sharp.Tests/MockedRepositoryFixture.cs b/LibGit2Sharp.Tests/MockingFixture.cs index 5161ff9b..13376fae 100644 --- a/LibGit2Sharp.Tests/MockedRepositoryFixture.cs +++ b/LibGit2Sharp.Tests/MockingFixture.cs @@ -7,11 +7,12 @@ using Xunit; 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 + // This fixture shows how one can mock various LibGit2Sharp APIs. + public class MockingFixture : BaseFixture { + // 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. + // 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] @@ -67,5 +68,42 @@ namespace LibGit2Sharp.Tests get { return repo.Commits.Count(); } } } + + [Fact] + public void CanFakeConfigurationBuildSignature() + { + var name = "name"; + var email = "email"; + var now = DateTimeOffset.UtcNow; + + var fakeConfig = new Mock<Configuration>(); + fakeConfig.Setup(c => c.BuildSignature(now)) + .Returns<DateTimeOffset>(t => new Signature(name, email, t)); + + var sig = fakeConfig.Object.BuildSignature(now); + Assert.Equal(name, sig.Name); + Assert.Equal(email, sig.Email); + Assert.Equal(now, sig.When); + } + + [Fact] + public void CanFakeEnumerationOfConfiguration() + { + var fakeConfig = new Mock<Configuration>(); + fakeConfig.Setup(c => c.GetEnumerator()).Returns(FakeEntries); + + Assert.Equal(2, fakeConfig.Object.Count()); + } + + private static IEnumerator<ConfigurationEntry<string>> FakeEntries() + { + yield return FakeConfigurationEntry("foo", "bar", ConfigurationLevel.Local); + yield return FakeConfigurationEntry("baz", "quux", ConfigurationLevel.Global); + } + + private static ConfigurationEntry<string> FakeConfigurationEntry(string key, string value, ConfigurationLevel level) + { + return new Mock<ConfigurationEntry<string>>(key, value, level).Object; + } } } |