Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise exception if you try to mock a non-virtual method #131

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Source/Delphi.Mocks.Proxy.pas
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,10 @@ function TProxy<T>._AddRef: Integer;

function TProxy<T>._Release: Integer;
begin
if FSetupMode <> TSetupMode.None then begin
ClearSetupState;
raise EMockSetupException.Create('Setup called on non-virtual method');
end;
result := inherited;
end;

Expand Down
17 changes: 17 additions & 0 deletions Tests/Delphi.Mocks.Tests.ObjectProxy.pas
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ TCommand = class
procedure TestVarParam(var msg : string);virtual;abstract;
procedure TestOutParam(out msg : string);virtual;abstract;
function VirtualMethod: Integer; virtual;
function NonVirtualMethod: Integer;
end;

{$M+}
Expand Down Expand Up @@ -65,6 +66,8 @@ TTestObjectProxy = class
procedure TestVarParam;
[Test]
procedure MockNoBehaviorDefined;
[Test]
procedure WillRaiseMockNonVirtualMethod;
end;
{$M-}

Expand Down Expand Up @@ -249,6 +252,15 @@ procedure TTestObjectProxy.MockNoBehaviorDefined;
mock.Verify;
end;

procedure TTestObjectProxy.WillRaiseMockNonVirtualMethod;
var
mock : TMock<TCommand>;
begin
mock := TMock<TCommand>.Create;
Assert.WillRaise(procedure begin mock.Setup.Expect.Once.When.NonVirtualMethod; end);
Assert.WillRaise(procedure begin mock.Setup.WillReturn(2).When.NonVirtualMethod; end);
end;

procedure TTestObjectProxy.MockWithArgProcedureUsingOnce;
var
mock : TMock<TCommand>;
Expand Down Expand Up @@ -282,6 +294,11 @@ constructor TMultipleConstructor.Create;

{ TCommand }

function TCommand.NonVirtualMethod: Integer;
begin
Result := 1;
end;

function TCommand.VirtualMethod: Integer;
begin
Result := 1;
Expand Down