-
Notifications
You must be signed in to change notification settings - Fork 2
/
FullSUTMock.cs
81 lines (65 loc) · 2.2 KB
/
FullSUTMock.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
namespace Examples;
using System;
using FluentAssertions;
using NSubstitute;
using Xunit;
public interface IFullMockService
{
string GetValue(Guid id);
}
public class FullSUTMock
{
private readonly IFullMockService _service;
public FullSUTMock(IFullMockService service)
{
_service = service;
}
public string GetValue(Guid id)
{
// Here we would like to make sure that we're able to stub other virtual methods of the same class.
// We wanna be sure that we get a partial mock which is useful for checking a scenario
// in which a method knows an algorithm. It doesn't know any lower level details
// it just does something according to some algorithm.
// In that algorithm it may call other services which were injected and we can stub(we're not concerned about those here)
// it also may call some other members of this class which could be virtual or quite often they're
// "protected virtual internal" those methods just may do some low level things so that our algorithm
// looks nice and clean.
//
// In this particular example the algorithm is pretty dumb it just delegates call to GetValueEx
// We would like be able to stub completely the call if we wish.
return GetValueEx(id);
}
protected internal virtual string GetValueEx(Guid id)
{
throw new NotImplementedException();
}
}
public class FullMockTests : TestsSubstituteOf<FullSUTMock>
{
[Fact]
public void MethodIsNotSubbed_OriginalImplementationWasNotCalled()
{
var id = Guid.NewGuid();
var wrapper = new Wrapper();
Use(wrapper);
var actual = SUT.GetValue(id);
actual.Should().BeNullOrWhiteSpace();
}
[Fact]
public void MethodIsSubbed_TheValueWhichWasSetOnStubbedIsReturned()
{
var id = Guid.NewGuid();
var wrapper = new Wrapper();
Use(wrapper);
SUT.GetValueEx(id).Returns("42");
var actual = SUT.GetValue(id);
actual.Should().Be("42");
}
private class Wrapper : IFullMockService
{
public string GetValue(Guid id)
{
return id.ToString();
}
}
}