-
Notifications
You must be signed in to change notification settings - Fork 897
/
Copy pathMockingFixture.cs
110 lines (93 loc) · 3.79 KB
/
MockingFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Moq;
using Xunit;
namespace LibGit2Sharp.Tests
{
// 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]
public void CanCountCommitsWithConcreteRepository()
{
string path = SandboxBareTestRepo();
using (var repo = new Repository(path))
{
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<IQueryableCommitLog>(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(); }
}
}
[Fact]
public void CanFakeConfigurationBuildSignature()
{
const string name = "name";
const string 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;
}
}
}