Is it possible to unit test a class that calls P / Invoke calls?

I want to wrap a piece of code that uses the Windows implementation API in a neat little helper class, and, as usual, I'm looking for a way to pass the test first. However, although WindowsIdentity is a managed class, the LogonUser call, which is required to actually log on as another user, is an unmanaged function in the advapi32.dll file.

I think I can get around this by introducing an interface for my helper class to use and hide P / Invoke calls in the implementation, but testing this implementation will still be a problem. And you can imagine that the actual implementation of the impersonation in the test can be a bit problematic, given that the user really must exist in the system.

+5
source share
2 answers

: , .
, WinAPI ( , , ). "", .. WinAPI. , , - - , . , .

  • IWinAPIFacade ( WinAPI) CWinAPIFacade.
  • , IWinAPIFacade ,
  • , , CWinAPIFacade ( ).
  • CWinAPIFacade, Platform Invoke - . . , . , , .
+10

, . PInvoke ( ), , - ?

, - ?

.., , , . PInvoke-, :)

0

All Articles