gpt4 book ai didi

delphi - 如何使用 Delphi-Mocks 框架在 Delphi 的子类中使用模拟

转载 作者:行者123 更新时间:2023-12-03 15:31:00 28 4
gpt4 key购买 nike

好的,我一直在用优秀的Delphi-Mocks Framework我刚刚遇到了一个问题。假设我有以下接口(interface):

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;

现在,我尝试使用 DUnit 和 Delphi-Mocks 测试 Sale 接口(interface),并按如下方式使用它:

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;

所以我的问题是:如何将子模拟添加到现有的模拟接口(interface)并调用其方法和属性?

谢谢!附:我正在使用Delphi XE2。

最佳答案

mckSale.Setup.WillReturnDefault('getDepartment', TValue.From(mckDepartment));

关于delphi - 如何使用 Delphi-Mocks 框架在 Delphi 的子类中使用模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10490776/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com