Skip to content

Commit

Permalink
Fixed issue #74 Stub WillReturnDefault
Browse files Browse the repository at this point in the history
Not sure why the behaviour was different to mocks, as automocking also wouldn't have worked.
  • Loading branch information
vincentparrett committed Apr 10, 2020
1 parent a5bb737 commit e9099dc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
26 changes: 15 additions & 11 deletions Delphi.Mocks.MethodData.pas
Original file line number Diff line number Diff line change
Expand Up @@ -534,17 +534,21 @@ procedure TMethodData.RecordHit(const Args: TArray<TValue>; const returnType : T

procedure TMethodData.StubNoBehaviourRecordHit(const Args: TArray<TValue>; const AExpectationHitCtr : Integer; const returnType: TRttiType; out Result: TValue);
begin
//If we have no return type defined, and the default return type is empty
if (returnType <> nil) and (FReturnDefault.IsEmpty) then
begin
//Return the default value for the passed in return type
Result := GetDefaultValue(returnType);
end
else if FSetupParameters.BehaviorMustBeDefined and (AExpectationHitCtr = 0) and (FReturnDefault.IsEmpty) then
begin
//If we must have default behaviour defined, and there was nothing defined raise a mock exception.
raise EMockException.Create(Format('[%s] has no behaviour or expectation defined for method [%s]', [FTypeName, FMethodName]));
end;
MockNoBehaviourRecordHit(Args, AExpectationHitCtr, returnType, Result);

//
// //If we have no return type defined, and the default return type is empty
// if (returnType <> nil) and (FReturnDefault.IsEmpty) then
// begin
// //Return the default value for the passed in return type
// Result := GetDefaultValue(returnType);
// end
// else if FSetupParameters.BehaviorMustBeDefined and (AExpectationHitCtr = 0) and (FReturnDefault.IsEmpty) then
// begin
// //If we must have default behaviour defined, and there was nothing defined raise a mock exception.
// raise EMockException.Create(Format('[%s] has no behaviour or expectation defined for method [%s]', [FTypeName, FMethodName]));
// end;
// result := FReturnDefault;
end;

function TMethodData.Verify(var report : string) : boolean;
Expand Down
47 changes: 47 additions & 0 deletions Tests/Delphi.Mocks.Tests.Stubs.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
unit Delphi.Mocks.Tests.Stubs;

interface

uses
DUnitX.TestFramework;
type
{$M+}
[TestFixture]
TStubTests = class
published
procedure Test_WillReturnDefault;
end;
{$M-}

{$M+}
ITestable = interface
function DoSomething(const value : string) : string;
end;
{$M-}


implementation

uses
Delphi.Mocks;
{ TUtilsTests }
{ TStubTests }

procedure TStubTests.Test_WillReturnDefault;
var
stub : TStub<ITestable>;
intf : ITestable;
actual : string;
begin
stub := TStub<ITestable>.Create;
stub.Setup.WillReturnDefault('DoSomething','hello');
intf := stub.Instance;
actual := intf.DoSomething('world');
Assert.AreEqual('hello', actual);
end;

initialization
TDUnitX.RegisterTestFixture(TStubTests);


end.
6 changes: 2 additions & 4 deletions Tests/Delphi.Mocks.Tests.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ uses
DUnitX.Windows.Console,
DUnitX.Loggers.XML.NUnit,
SysUtils,
{$IFDEF TESTINSIGHT}
TestInsight.DUnitX,
{$ENDIF }
Delphi.Mocks.AutoMock in '..\Delphi.Mocks.AutoMock.pas',
Delphi.Mocks.Behavior in '..\Delphi.Mocks.Behavior.pas',
Delphi.Mocks.Expectation in '..\Delphi.Mocks.Expectation.pas',
Expand Down Expand Up @@ -63,7 +60,8 @@ uses
Delphi.Mocks.Tests.TValue in 'Delphi.Mocks.Tests.TValue.pas',
Delphi.Mocks.Tests.Utils in 'Delphi.Mocks.Tests.Utils.pas',
Delphi.Mocks.Utils.Tests in 'Delphi.Mocks.Utils.Tests.pas',
Delphi.Mocks.Examples.Matchers in 'Delphi.Mocks.Examples.Matchers.pas';
Delphi.Mocks.Examples.Matchers in 'Delphi.Mocks.Examples.Matchers.pas',
Delphi.Mocks.Tests.Stubs in 'Delphi.Mocks.Tests.Stubs.pas';

{$R *.RES}

Expand Down

0 comments on commit e9099dc

Please sign in to comment.