This repository has been archived by the owner on Oct 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathexample.d
97 lines (78 loc) · 1.72 KB
/
example.d
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
/**
* Imports.
*/
import dunit.mockable;
import std.algorithm;
/**
* Simple class representing a person.
*/
class Person
{
private string _name;
private int _age;
this()
{
}
this(string name, int age)
{
this._name = name;
this._age = age;
}
public string getName()
{
return this._name;
}
public int getAge()
{
return this._age;
}
// Mixin mocking behaviour.
mixin Mockable!(Person);
}
/**
* Processor class that uses Person as a dependency.
*/
class Processor
{
private Person[] _people;
public void addPerson(Person person)
{
this._people ~= person;
}
public ulong getAmountOfPeople()
{
return this._people.length;
}
public float getMeanAge()
{
return cast(float)reduce!((a, b) => a + b.getAge())(0, this._people) / this.getAmountOfPeople();
}
}
unittest
{
import dunit.toolkit;
// Create mock people.
auto gary = Person.getMock();
gary.disableParentMethods();
auto tessa = Person.getMock();
tessa.disableParentMethods();
// Mock the getAge method to return 40. Set the minimum count to 1 and the maximum count to 2.
gary.mockMethod("getAge", delegate(){
return 40;
}, 1, 2);
// Mock the getAge method to return 34. Set the minimum count to 1 and the maximum count to 2.
tessa.mockMethod("getAge", delegate(){
return 34;
}, 1, 2);
// Create the object we are unit testing.
auto processor = new Processor();
// Add mock people to the processor.
processor.addPerson(gary);
processor.addPerson(tessa);
// Make assertions of the processor, calling the mock methods on the mock class.
processor.getAmountOfPeople().assertEqual(2);
processor.getMeanAge().assertEqual(37);
// Assert mock method calls are within limits.
gary.assertMethodCalls();
tessa.assertMethodCalls();
}