Skip to content

Commit e9099dc

Browse files
Fixed issue #74 Stub WillReturnDefault
Not sure why the behaviour was different to mocks, as automocking also wouldn't have worked.
1 parent a5bb737 commit e9099dc

File tree

3 files changed

+64
-15
lines changed

3 files changed

+64
-15
lines changed

Delphi.Mocks.MethodData.pas

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -534,17 +534,21 @@ procedure TMethodData.RecordHit(const Args: TArray<TValue>; const returnType : T
534534

535535
procedure TMethodData.StubNoBehaviourRecordHit(const Args: TArray<TValue>; const AExpectationHitCtr : Integer; const returnType: TRttiType; out Result: TValue);
536536
begin
537-
//If we have no return type defined, and the default return type is empty
538-
if (returnType <> nil) and (FReturnDefault.IsEmpty) then
539-
begin
540-
//Return the default value for the passed in return type
541-
Result := GetDefaultValue(returnType);
542-
end
543-
else if FSetupParameters.BehaviorMustBeDefined and (AExpectationHitCtr = 0) and (FReturnDefault.IsEmpty) then
544-
begin
545-
//If we must have default behaviour defined, and there was nothing defined raise a mock exception.
546-
raise EMockException.Create(Format('[%s] has no behaviour or expectation defined for method [%s]', [FTypeName, FMethodName]));
547-
end;
537+
MockNoBehaviourRecordHit(Args, AExpectationHitCtr, returnType, Result);
538+
539+
//
540+
// //If we have no return type defined, and the default return type is empty
541+
// if (returnType <> nil) and (FReturnDefault.IsEmpty) then
542+
// begin
543+
// //Return the default value for the passed in return type
544+
// Result := GetDefaultValue(returnType);
545+
// end
546+
// else if FSetupParameters.BehaviorMustBeDefined and (AExpectationHitCtr = 0) and (FReturnDefault.IsEmpty) then
547+
// begin
548+
// //If we must have default behaviour defined, and there was nothing defined raise a mock exception.
549+
// raise EMockException.Create(Format('[%s] has no behaviour or expectation defined for method [%s]', [FTypeName, FMethodName]));
550+
// end;
551+
// result := FReturnDefault;
548552
end;
549553

550554
function TMethodData.Verify(var report : string) : boolean;

Tests/Delphi.Mocks.Tests.Stubs.pas

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
unit Delphi.Mocks.Tests.Stubs;
2+
3+
interface
4+
5+
uses
6+
DUnitX.TestFramework;
7+
type
8+
{$M+}
9+
[TestFixture]
10+
TStubTests = class
11+
published
12+
procedure Test_WillReturnDefault;
13+
end;
14+
{$M-}
15+
16+
{$M+}
17+
ITestable = interface
18+
function DoSomething(const value : string) : string;
19+
end;
20+
{$M-}
21+
22+
23+
implementation
24+
25+
uses
26+
Delphi.Mocks;
27+
{ TUtilsTests }
28+
{ TStubTests }
29+
30+
procedure TStubTests.Test_WillReturnDefault;
31+
var
32+
stub : TStub<ITestable>;
33+
intf : ITestable;
34+
actual : string;
35+
begin
36+
stub := TStub<ITestable>.Create;
37+
stub.Setup.WillReturnDefault('DoSomething','hello');
38+
intf := stub.Instance;
39+
actual := intf.DoSomething('world');
40+
Assert.AreEqual('hello', actual);
41+
end;
42+
43+
initialization
44+
TDUnitX.RegisterTestFixture(TStubTests);
45+
46+
47+
end.

Tests/Delphi.Mocks.Tests.dpr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ uses
2626
DUnitX.Windows.Console,
2727
DUnitX.Loggers.XML.NUnit,
2828
SysUtils,
29-
{$IFDEF TESTINSIGHT}
30-
TestInsight.DUnitX,
31-
{$ENDIF }
3229
Delphi.Mocks.AutoMock in '..\Delphi.Mocks.AutoMock.pas',
3330
Delphi.Mocks.Behavior in '..\Delphi.Mocks.Behavior.pas',
3431
Delphi.Mocks.Expectation in '..\Delphi.Mocks.Expectation.pas',
@@ -63,7 +60,8 @@ uses
6360
Delphi.Mocks.Tests.TValue in 'Delphi.Mocks.Tests.TValue.pas',
6461
Delphi.Mocks.Tests.Utils in 'Delphi.Mocks.Tests.Utils.pas',
6562
Delphi.Mocks.Utils.Tests in 'Delphi.Mocks.Utils.Tests.pas',
66-
Delphi.Mocks.Examples.Matchers in 'Delphi.Mocks.Examples.Matchers.pas';
63+
Delphi.Mocks.Examples.Matchers in 'Delphi.Mocks.Examples.Matchers.pas',
64+
Delphi.Mocks.Tests.Stubs in 'Delphi.Mocks.Tests.Stubs.pas';
6765

6866
{$R *.RES}
6967

0 commit comments

Comments
 (0)