I would like to write unit test for wmi c# code, but couldn't mock managementObjectCollection class

shuwpan 41 Reputation points
2022-06-28T17:46:49.677+00:00

Hi,

I wrote a method to uinistall one of our services running on windows.
I am using ManagementObjectSearcher to filter out the service and invoke "Uninstall" method.
There is code coverage requirement need to be meet. I need to write a UT to get coverage.
I am using Microsoft's fakes framework.
I would like to mock the returned collection from search.Get(). I am able to use reflection to get the constructor, however, couldn't initialized it properly.
{Void .ctor(System.Management.ManagementScope, System.Management.EnumerationOptions, System.Management.IEnumWbemClassObject)}
I don't know how to create the 3rd parameter, and not sure how to populate the collection.
Any help is appreciated, thanks in advance.

Thanks,
Shuwpan

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
Windows Server Management
Windows Server Management
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Management: The act or process of organizing, handling, directing or controlling something.
421 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
328 questions
{count} votes