您不需要公開它們。受保護會做。然後,您可以爲類進行子類型的單元測試,並顯示受保護的方法。例如:
type
TAuth = class(TDataModule)
protected
procedure MethodIWantToUnitTest;
public
procedure PublicMethod;
end;
現在你可以亞型它爲你的單元測試:
interface
uses
TestFramework, Classes, AuthDM;
type
// Test methods for class TAuthDM
TestAuthDM = class(TTestCase)
// stuff
end;
TAuthDMTester = class(TAuthDM)
public
procedure MethodIWantToUnitTestMadePublic;
end;
implementation
procedure TAuthDMTester.MethodIWantToUnitTestMadePublic;
begin
MethodIWantToUnitTest;
end;
但是,如果進行單元測試所需的方法與數據模塊做事如此密切,它是不是安全除了私有的東西外,你應該考慮重構這些方法,以便分離需要進行單元測試的代碼和訪問數據模塊內部的代碼。
另請參見:[使用JUnit使用私有方法測試類的正確方法是什麼?](http://stackoverflow.com/questions/34571/whats-the-proper-way-to-test-a-class -with-private-methods-using-junit) – mjn 2012-11-15 12:24:14
另一個選項http://stackoverflow.com/questions/7525071/accesing-a-strict-private-field-using-the-rtti – sav 2014-12-11 02:21:51
可能的重複[如何測試一個具有私有方法,字段或內部類的類?](https://stackoverflow.com/questions/34571/how-do-i-test-a-class-that-has-private-methods-fields-or-內部類) – Raedwald 2017-12-14 13:08:02