Skip to content

Commit 8fbdd67

Browse files
Merge pull request #131 from mendrix/master
Raise exception if you try to mock a non-virtual method
2 parents 356f4a1 + 3f92f6b commit 8fbdd67

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Source/Delphi.Mocks.Proxy.pas

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,10 @@ function TProxy<T>._AddRef: Integer;
880880

881881
function TProxy<T>._Release: Integer;
882882
begin
883+
if FSetupMode <> TSetupMode.None then begin
884+
ClearSetupState;
885+
raise EMockSetupException.Create('Setup called on non-virtual method');
886+
end;
883887
result := inherited;
884888
end;
885889

Tests/Delphi.Mocks.Tests.ObjectProxy.pas

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ TCommand = class
3333
procedure TestVarParam(var msg : string);virtual;abstract;
3434
procedure TestOutParam(out msg : string);virtual;abstract;
3535
function VirtualMethod: Integer; virtual;
36+
function NonVirtualMethod: Integer;
3637
end;
3738

3839
{$M+}
@@ -65,6 +66,8 @@ TTestObjectProxy = class
6566
procedure TestVarParam;
6667
[Test]
6768
procedure MockNoBehaviorDefined;
69+
[Test]
70+
procedure WillRaiseMockNonVirtualMethod;
6871
end;
6972
{$M-}
7073

@@ -249,6 +252,15 @@ procedure TTestObjectProxy.MockNoBehaviorDefined;
249252
mock.Verify;
250253
end;
251254

255+
procedure TTestObjectProxy.WillRaiseMockNonVirtualMethod;
256+
var
257+
mock : TMock<TCommand>;
258+
begin
259+
mock := TMock<TCommand>.Create;
260+
Assert.WillRaise(procedure begin mock.Setup.Expect.Once.When.NonVirtualMethod; end);
261+
Assert.WillRaise(procedure begin mock.Setup.WillReturn(2).When.NonVirtualMethod; end);
262+
end;
263+
252264
procedure TTestObjectProxy.MockWithArgProcedureUsingOnce;
253265
var
254266
mock : TMock<TCommand>;
@@ -282,6 +294,11 @@ constructor TMultipleConstructor.Create;
282294

283295
{ TCommand }
284296

297+
function TCommand.NonVirtualMethod: Integer;
298+
begin
299+
Result := 1;
300+
end;
301+
285302
function TCommand.VirtualMethod: Integer;
286303
begin
287304
Result := 1;

0 commit comments

Comments
 (0)