How to use mocks in child classes in Delphi using Delphi-Mocks framework
delphi, delphi-mocks, mocking
Solution
mckSale.Setup.WillReturnDefault('getDepartment', TValue.From(mckDepartment));
Problem
Ok, I have been using the excellent Delphi-Mocks Framework and I just encountered a problem. Let´s suppose I have the following interfaces: ``` IDepartment = Interface ['{F4915950-8F32-4944-A3B6-8E08F6C38ECC}'] function getID() : Integer; function getName() : String; property ID: Integer read getID; property Name: String read getName; end; ISale = Interface ['{F4915950-8F32-4944-A3B6-8E08F6C38E77}'] function getAmmount() : Currency; function getDepartment() : IDepartment; property Ammount: Currency read getAmmount; property Department : IDepartment getDepartment; end; ``` Now, I am trying to test the Sale interface using DUnit and the Delphi-Mocks, and use it as follows: ``` procedure TMyTest.Test_Sale_HasDepartment; var mckDepartment : TMock<IDeparment>; mckSale : TMock<ISale>; begin mckDepartment := TMock<IDepartment>.Create; mckDepartment.Setup.WillReturn('My Department').When.Name; mckDepartment.Setup.WillReturn(1).When.ID; // Create a sale Mock mckSale := TMock<ISale>.Create; mckSale.Setup.WillReturn(100).When.Ammount; //** Here´s there is where I don´t know how to add a "child mock"** mckSale.Setup.WillReturn(TValue.From<IDepartment>(mckDepartment)).When.Department; // What I am trying to get is the following: WriteLn(mckSale.Instance.Department.Name); // Should return "My Department" but fails with an AV. end; ``` So my question is: How can I add a child mock to an existing mocked Interface and call its methods and properties? Thanks! P.S. I am using Delphi XE2.