-
Notifications
You must be signed in to change notification settings - Fork 1
/
MockTest.pas
105 lines (84 loc) · 2.25 KB
/
MockTest.pas
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
unit MockTest;
interface
uses
DUnitX.TestFramework, Delphi.Mocks, AutoFixture;
type
TDaoClass = class
public function saveObjectToDb(aObject: String): Boolean; virtual;
end;
TControllerClass = class
private
Fdao: TDaoClass;
FId: Integer;
public
property Id:Integer read Fid write Fid;
function process(aObject: String): Boolean;
constructor Create(aDao: TDaoClass); reintroduce;
end;
[TestFixture]
TMockTest = class(TObject)
private
UUT: TControllerClass;
//FMockDao: TMock<TDaoClass>;
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
// Sample Methods
// Simple single Test
[Test]
procedure Test1;
// Test with TestCase Attribute to supply parameters.
[Test]
[TestCase('TestA','1,2')]
[TestCase('TestB','3,4')]
procedure Test2(const AValue1 : Integer;const AValue2 : Integer);
end;
implementation
procedure TMockTest.Setup;
//var vUUTMock: TMock<TControllerClass>;
var vFixture: TAutoFixture;
begin
vFixture := TAutoFixture.Create;
// FMockDao := TMock<TDaoClass>.Create;
// FMockDao.Setup.WillReturn(True).When.saveObjectToDb('TEST');
// vFixture.Inject(FMockDao.Instance);
vFixture.Mock<TDaoClass>.Setup.WillReturn(True).When.saveObjectToDb('TEST');
UUT := vFixture.New<TControllerClass>;
//vUUTMock := TMock<TControllerClass>.Create;
//UUT := vUUTMock;
end;
procedure TMockTest.TearDown;
begin
end;
procedure TMockTest.Test1;
begin
Assert.IsTrue(Assigned(UUT), 'Fdao was not created');
end;
procedure TMockTest.Test2(const AValue1 : Integer;const AValue2 : Integer);
var vResult: Boolean;
begin
// Act
vResult := UUT.process('TEST');
// Assert
Assert.IsTrue(vResult, 'Process not returning success (true)');
end;
{ TControllerClass }
constructor TControllerClass.Create(aDao: TDaoClass);
begin
inherited Create;
FDao := aDao;
end;
function TControllerClass.process(aObject: String): Boolean;
begin
Result := FDao.saveObjectToDb(aObject);
end;
{ TDaoClass }
function TDaoClass.saveObjectToDb(aObject: String): Boolean;
begin
Result := (aObject = '');
end;
initialization
TDUnitX.RegisterTestFixture(TMockTest);
end.