tags:

views:

72

answers:

2

I have started writing web services in Delphi 2010 and am unit testing to make sure they function as planned. My unit tests of the code passed but one web service method didn’t return a value when called as a service (i.e. through SoapUI). After many hours of searching through the code I discovered it was because the properties on my return object weren’t in the published section of the interface; they were in the public section.

Is there a way for my unit tests to check variable visibility on objects so I can avoid this problem in the future? I was trying to find a way with RTTI but haven’t been able to find anything.

+6  A: 

You can determine whether a property was declared published by attempting to access that property's RTTI. A public property has no RTTI, a published property does.

Something like this:

if (GetPropInfo(myobject, "PropertyName") != null) then 
    // it's published...

For more info on RTTI, see Brian Long's article: http://www.blong.com/Conferences/BorConUK98/DelphiRTTI/CB140.htm

dthorpe
In D2010+, non-published items are now exposed in the new Enhanced RTTI system (Rtti.pas). The old-style RTTI (TypInfo.pas) still only deals in published items, yes (BTW, there is an IsPublishedProp() function available).
Remy Lebeau - TeamB
Thanks Remy, I had forgotten about the IsPublishedProp helper function. Answering core RTL tech questions purely from memory gets to be a little tricky after 5+ years in the attic. ;>
dthorpe
Thanks for pointing me towards enhanced RTTI. It worked out great.
Sam M
+3  A: 

You can do it with RTTI easily enough. You can use the classic RTTI function GetPropInfo in the TypInfo unit. If it returns nil, then no published property by that name exists. Or you can look it up with extended RTTI and check the Visibility property, which will tell you what visibility level it's declared at.

Mason Wheeler