0

I'm writing an internal nuget package for my company that will allow applications in our suite to speak to specific bluetooth devices for certain workflows. Pretty cool stuff!

But as I began the project, I tried to take the opportunity to run TDD as I was starting from scratch. It didn't take me very long to realize something strange to me:

The interfaces that the classes in the Windows.Devices namespace are all marked as internal.

I've been pondering this for a couple of days, but I can't get away from asking the following question: Does this mean that anyone using bluetooth for Windows 8/10 not writing ANY tests where these services are mocked? I'm having to wrap/adapt every single one of these classes because their construction is not trivial. Then write an interface for the wrapper/adapter that is basically just a mirror of the interface that I should be able to use in my mind...

Questions:

  • Aren't interfaces meant to be public?
  • What purpose do they serve if they don't share the same accessibility as their implementations?
  • IS anyone writing unit tests for code that utilizes these libraries?
  • Am I missing something, and there is a way to mock the interfaces of these classes despite them being internal?
Community
  • 1
  • 1
Jake Smith
  • 2,332
  • 1
  • 30
  • 68
  • Perhaps you could share a link to the source code of such an interface? – mjwills Dec 04 '18 at 23:40
  • 1
    Just smoke and mirrors implemented by the WinRT language projection. This is code written in C++ and exposed through COM. You can see the real declaration by looking at, say, C:\Program Files (x86)\Windows Kits\10\Include\10.0.14393.0\winrt\windows.devices.bluetooth.idl. The [exclusiveto] attribute ensures the interface is hidden and the members can only be accessed through the runtimeclass. Backgrounder on the IDL stuff [is here](https://learn.microsoft.com/en-us/uwp/midl-3/synthesizing-interfaces). – Hans Passant Dec 05 '18 at 00:02
  • I'm confused by this question. If the interface is internal, then the class under test cannot have a variable declared with it as its type. If the class under test doesn't have it, why do you need to mock it? You'd only need to mock it if you were testing Microsoft's implementation, not yours. Perhaps I am not understanding. Can you post a code sample to clarify? – John Wu Dec 05 '18 at 00:37
  • I have since adapted a lot of the classes from Microsoft, so it would be a little difficult/too much code to post here. But basically, without adapting their classes with my own wrappers and interfaces, the problem is that, for instance, the class `DeviceInformation` is not easily constructable in a test, and the several interfaces it descends from are marked as internal. In fact, it is really annoying when Resharper suggests using the interface as the type for parameters instead of the implementation, but then complains about it being internal when I say, "sure, Resharper..." – Jake Smith Dec 05 '18 at 16:36
  • @JohnWu, You nailed the problem right on the head. I can't use the interfaces as the reference type, because they are internal. So then, then code using the references to implementation types are impossible to test because the construction of those types are nearly impossible as far as I'm concerned. – Jake Smith Dec 05 '18 at 16:37
  • @HansPassant - so does that seem to indicate that code using these types is inherently not testable in a C# test project consuming that C# class library? – Jake Smith Dec 05 '18 at 16:38
  • @mjwills, I couldn't find the source online to prove that the interfaces are marked as internal, but resharper can show a representation of the source if you "F12" the type. Then you will see a class declaration similar to what you see [here](https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothdevice). The interfaces `IBluetoothDevice, IBluetoothDevice2, IBluetoothDevice3, IBluetoothDevice4, IBluetoothDevice5` in that declaration are all internal and not able to be references in my assembly. My question was around wanting to mock these devices for testing purposes. – Jake Smith Dec 05 '18 at 16:42

0 Answers0