-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathThingCache.cs
More file actions
111 lines (95 loc) · 3.52 KB
/
ThingCache.cs
File metadata and controls
111 lines (95 loc) · 3.52 KB
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
111
using System.Collections.Generic;
using FakeItEasy;
using NUnit.Framework;
namespace MockFramework
{
public class ThingCache
{
private readonly IDictionary<string, Thing> dictionary
= new Dictionary<string, Thing>();
private readonly IThingService thingService;
public ThingCache(IThingService thingService)
{
this.thingService = thingService;
}
public Thing Get(string thingId)
{
Thing thing;
if (dictionary.TryGetValue(thingId, out thing))
return thing;
if (thingService.TryRead(thingId, out thing))
{
dictionary[thingId] = thing;
return thing;
}
return null;
}
}
[TestFixture]
public class ThingCache_Should
{
private IThingService thingService;
private ThingCache thingCache;
private const string thingId1 = "TheDress";
private Thing thing1 = new Thing(thingId1);
private const string thingId2 = "CoolBoots";
private Thing thing2 = new Thing(thingId2);
private const string thingIdNotExisted = "NotExisted";
private Thing thing3 = null;
// Метод, помеченный атрибутом SetUp, выполняется перед каждым тестов
[SetUp]
public void SetUp()
{
thingService = A.Fake<IThingService>();
A.CallTo(() => thingService.TryRead(thingId1, out thing1)).Returns(true);
A.CallTo(() => thingService.TryRead(thingId2, out thing2)).Returns(true);
A.CallTo(() => thingService.TryRead(thingIdNotExisted, out thing3)).Returns(false);
thingCache = new ThingCache(thingService);
}
// TODO: Написать простейший тест, а затем все остальные
// Live Template tt работает!
// Пример теста
[Test]
public void ThingCache_Get_ReturnsCorrectValue()
{
var actualValue = thingCache.Get(thingId1);
Assert.That(actualValue, Is.EqualTo(thing1));
}
[Test]
public void ThingCache_Get_ReturnsNoValue()
{
var notExistedValue = thingCache.Get(thingIdNotExisted);
Assert.That(notExistedValue, Is.Null);
}
[Test]
public void ThingCache_Get_MethodCallingHappened()
{
thingCache.Get(thingId1);
A.CallTo(() => thingService.TryRead(thingId1, out thing1)).MustHaveHappenedOnceExactly();
}
/** Проверки в тестах
* Assert.AreEqual(expectedValue, actualValue);
* actualValue.Should().Be(expectedValue);
*/
/** Синтаксис AAA
* Arrange:
* var fake = A.Fake<ISomeService>();
* A.CallTo(() => fake.SomeMethod(...)).Returns(true);
* Assert:
* var value = "42";
* A.CallTo(() => fake.TryRead(id, out value)).MustHaveHappened();
*/
/** Синтаксис out
* var value = "42";
* string _;
* A.CallTo(() => fake.TryRead(id, out _)).Returns(true)
* .AssignsOutAndRefParameters(value);
* A.CallTo(() => fake.TryRead(id, out value)).Returns(true);
*/
/** Синтаксис Repeat
* var value = "42";
* A.CallTo(() => fake.TryRead(id, out value))
* .MustHaveHappened(Repeated.Exactly.Twice)
*/
}
}