3

I'm attempting to write a unit test for a simple factory class that creates one of several possible implementing objects and returns it as an interface reference.

DUnit has a built in procedure, CheckIs(AObject: TObject; AClass: TClass; msg: string), that based on its name and the parameters it accepts should fail the test if the object's class type doesn't match the expected one. The only problem is it requires an object reference not an interface reference.

So I'm trying to use CheckTrue and perform the comparison in the body of the test but I'm not as familiar with Delphi's type checking support as I am with C#'s.

I know the is operator is out of the question since it only works with object references.

CheckTrue(LMyInterfaceReference {comparison here} TMyClass);

Any suggestions?

BTW, I'm using Delphi 2009 so I don't have access to the new RTTI support added in 2010+.

6
  • 2
    Delphi 2010 lets you do Obj := IUnknown as TObject Mar 4, 2011 at 17:01
  • 1
    Since you are using Delphi 2009 you'll need Barry Kelly's workaround, published very recently right here on Stack Overflow! Not posting as an answer since that would feel like stealing rep from Barry! Mar 4, 2011 at 17:08
  • @codeelegance, Barry's hack + an optional IFEDF when you upgrade your Delphi version looks like a pretty good deal. The hack works on Delphi 2009, and if you upgrade, you get the functionality (probably the same code) out-of-the-box. Mar 4, 2011 at 17:13
  • 1
    Instead of checking for a class, wouldn't it be more OOPish to check if a spcific interface is implemented?
    – mjn
    Mar 4, 2011 at 17:25
  • 1
    I'm guessing that OP is doing unit tests and wants to check that the implementing object is as expected. Mar 4, 2011 at 17:27

3 Answers 3

4

I'm wondering why you MUST have to test this... maybe you really don't have to.

But if knowing the underlying object of a Interface is a must, you have two choices:

  • Add a method to the interface which returns the underlying object, just a TObject, and implement this in each class just by returning self.
  • Hack a bit, for example using this Interface to object routine.
6
  • If a class is expected to have a specific behavior why would you NOT test it? You pass a type code (string, enum, whatever) to a factory and expect it to give you a corresponding instance of the type you requested. If it return's nil or an instance of the wrong type you've got a bug. Mar 4, 2011 at 18:00
  • 3
    Codeelegance, if you're returning an interface, then check whether the interface is what was expected. If shouldn't matter what type the underlying object has, or even that there is an underlying object. As long as the interface behaves the way it's supposed to, why does the class matter? If the precise class type is important, then return classes, not interfaces. Mar 4, 2011 at 18:51
  • 1
    @Rob I am testing whether the factory behaves the way its suppose to. The consumer of the interface doesn't know or care about the implementation of the interface. It depends on the factory to ensure the correct implementation is created. That is what I am testing. Mar 4, 2011 at 19:37
  • 2
    The factory's job is to produce things that implement a particular interface. If the factory gives you something, and the methods you call on that thing do what they're supposed to, then the factory has done its job. You're testing at the wrong level. Mar 4, 2011 at 20:03
  • Wrong level? Show me class that doesn't need to be tested and I show you a class that isn't needed at all. You only need to test the stuff that you want to work. - Kent Beck Mar 4, 2011 at 22:44
4

If you don't like hacks and don't feel like upgrading to Delphi 2010+ you may use an interface like this:

IImplementingObjectInterface = interface
  function GetImplementingObject: TObject;
end;

Make sure your objects also implement this interface and use it to extract the implementing object. If you need to do this for a lot of objects you can define your own TInterfacedObject derivate that already implements this so you can simply change your inheritance and be done.

1
  • This one had me screaming in fustration. Feb 4, 2013 at 12:47
1

Barry Kelly (one of the main Embarcadero Delphi Compiler Engineers) wrote a nice An ugly alternative to interface to object casting this week.

It answers your question.

The fun is that Hallvard Vassbotn wrote a very similar piece of code back in 2004.

From Delphi 2010 on, you can just use an is check or as cast to go back from interface references to object references.

--jeroen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.