COM Interop and Unit Testing

When you are writing a class in .NET that is intended to do COM interop, i.e. taking COM objects as parameters, do your unit testing with actual COM objects.

When you tlbimp a type library, a class is created that is based on the COM object’s class interface.

Do use this class to instantiate the desired COM object.

Do not use this class for anything else.

Do not declare members or parameters using this class.

Do not cast inbound parameters to this class.

Do use the most appropriate imported interface for a declaration and cast to another interface if necessary.

As long as you’re unit testing with new myComClass instances, everything will look pretty fine. But once you’re actually facing a __ComObject instance around a IMyDefaultClassInterface your code will fail.

Do use an unmanaged C++/VB6 test harness for unit testing .NET COM interop.

This entry was posted in Software Development. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s