delphi - How do you unit-test code that interacts with and instantiates third-party COM objects? -
one of biggest issues holding me diving full steam unit testing large percentage of code write heavily dependent on third-party com objects different sources tend interact each other (i'm writing add-ins microsoft office using several helper libraries if need know).
i know should use mock objects how go in case? can see it's relatively easy when have pass reference existing object of routines instantiate external com objects , pass them on other external com-object yet different library.
what best-practice approach here? should have testing code temporarily change com registration information in registry tested code instantiate 1 of mock objects instead? should inject modified type library units? other approaches there?
i grateful examples or tools delphi happy more general advice , higher-level explanations well.
thanks,
oliver
the traditional approach says client code should use wrapper, responsible instantiating com object. wrapper can mocked.
because you've got parts of code instantiating com objects directly, doesn't fit. if can change code, use factory pattern: use factory create com object. can mock factory return alternative objects.
whether object accessed via wrapper or via original com interface you. if choose mock com interface, remember instrument iunknown::queryinterface in mock, know you've mocked of interfaces, particularly if object passed other com object.
alternatively, check out cotreateasclass method. i've never used it, might need.
Comments
Post a Comment