How do you execute a single test code that interacts with third-party COM objects and creates them?

One of the biggest problems that currently keeps me from completely immersing myself in unit testing is that the really large percentage of the code that I write depends heavily on third-party COM objects from different sources, which also tend to interact with each other. with a friend (I am writing add-ons for Microsoft Office using several helper libraries if you need to know).

I know that I should probably use mock objects, but how exactly will I do this in this case? I see that this is relatively easy when I just need to pass a reference to an existing object, but some of my routines instantiate external COM objects and then sometimes pass them to another external COM object from another library.

What is the best approach? Should my test code temporarily change the COM registration information in the registry so that the tested code creates an instance of one of my mock objects? Should I introduce modified type library modules? What other approaches exist?

I would be especially grateful for examples or tools for Delphi, but would also be just as happy with more general tips and explanations of a higher level.

Thanks,

Oliver

+6
unit-testing delphi mocking com
source share
3 answers

The traditional approach says that your client code should use a wrapper that is responsible for instantiating the COM object. Then this wrapper can be easily ridiculed.

Since you have parts of your code that instantiate COM objects directly, this is not appropriate. If you can change this code, you can use the factory pattern: they use factory to create a COM object. You can make fun of factory to return alternative objects.

Access to the object through a wrapper or through the original COM interface is up to you. If you decide to mock the COM interface, remember the IUnknown :: QueryInterface tool in your layout, so you know that you mock all the interfaces, especially if the object is then passed to another COM object.

Alternatively check out the CoTreateAsClass method. I never used it, but it could do what you need.

+6
source share

It comes down to design for verification. Ideally, you should not instantiate these COM objects directly, but you should access them through an indirect layer, which can be replaced by a mock object.

Now COM itself provides a level of indirection, and you can provide a mock object that provided a replacement for the real one, but I suspect it would be painful to create, and I doubt that you will get much help from the existing mocking structure.

+3
source share

I would write a thin shell class around your third-party COM object that has the ability to load a mock object, rather than the actual COM object in a device testing situation. I usually do this with a second constructor, which I call passing in the layout object. The regular constructor just loaded the COM object as usual.

The Wikipedia article has a good introduction to the Wikipedia theme skillfully

+2
source share

All Articles