winforms icon indicating copy to clipboard operation
winforms copied to clipboard

Need a way to mock RegistryKey

Open hughbe opened this issue 5 years ago • 2 comments

  • .NET Core Version: master

  • Have you experienced this same bug with .NET Framework?: N/A

Problem description: We want to test various registry interactions such as with InputLanguage (https://github.com/dotnet/winforms/pull/3237)

@weltkante mentioned he's putting something together but this could take some time (https://github.com/dotnet/winforms/pull/3237#discussion_r422039848)

Opening this issue to track that mocking method

hughbe avatar May 08 '20 09:05 hughbe

extending the issue description a bit:

Problem description:

Testing failure cases of global APIs like registry or system settings is hard because they are often machine specific. Setting up dedicated test machines for different scenarios is not scalable.

Proposed solution:

In commercial solutions the profiler API is used for mocking static methods, non-virtual methods or sealed classes. This is done by running the test code under a profiler which can replace or redirect method bodies.

If .NET is the only consumer of the global API within the test scope (i.e. you aren't calling native methods which also accesses the global API) then you can use the profiler API to mock these methods and simulate failure scenarios.

Since this technique seems only be implemented in commercial test frameworks I've started working on an open source implementation so it can also be used in open source projects without relying on contributors having access to some commercial test framework.

It will be based on the recently open sourced CLRInstrumentationEngine repository which provides most of the low level details of implementing the profiler API and also allows to play well with other profiler extensions (historically only one client of the profiler API could be loaded)

weltkante avatar May 08 '20 10:05 weltkante

@hughbe I see you have been poking at CLRIE ;-) I pushed my proof of concept to a fork here if people are interested in how its done on a technical level. Like I said its still a bit early since only a low level API is implemented, but it works. It has a sandbox project as well, its Desktop Framework though, didn't get around to test .NET Core yet (but I don't think there's any issue, the API is the same and CLRIE has interest running on both frameworks).

Work left to be done is developing a high level API in the style of other mocking frameworks so you can comfortably use it from C# without a lot of boilerplate. Also need to separate the POC from CLRIE and discuss what low level parts will flow upstream and what remains separate.

weltkante avatar May 08 '20 12:05 weltkante