8

Windows Impersonation API を使用するコードを小さなヘルパー クラスにラップしたいと考えており、いつものように、テスト ファーストにする方法を探しています。ただし、WindowsIdentity はマネージ クラスですが、別のユーザーとして実際にログインを実行するために必要な LogonUser 呼び出しは、advapi32.dll のアンマネージ関数です。

ヘルパー クラスが使用するインターフェイスを導入し、実装で P/Invoke 呼び出しを非表示にすることで、これを回避できると思いますが、その実装をテストすることは依然として問題になります。また、ユーザーが実際にシステム上に存在する必要があることを考えると、テストで偽装を実際に実行することは少し問題になる可能性があることを想像できます。

4

2 に答える 2

12

Guideline: Don't test code that you haven't written.
You shouldn't be concerned with WinAPI implementation not working (most probably it works as expected). Your concern should be testing the 'Wiring' i.e. if your code makes the right WinAPI call. In which case, all you need is to mock out the interface and let the mock framework tell if you the call was made with the right params. If yes, you're done.

  • Create IWinAPIFacade (with relevant WinAPI methods) and implementation CWinAPIFacade.
  • Write a test which plugs in a mock of IWinAPIFacade and verify that the appropriate call is made
  • Write a test to ensure that CWinAPIFacade is created and plugged in as a default (in normal functioning)
  • Implement CWinAPIFacade which simply blind-delegates to Platform Invoke calls - no need to auto-test this layer. Just do a manual verification. Hopefully this won't change that often and nothing breaks. If you find that it does in the future, barricade it with some tests.
于 2008-09-10T05:12:00.897 に答える
0

I am not sure if I follow you.. You don't want to test the PInvoke yourself (you didn't write it) so you want to test that the wrapper class is performing as expected right?

So, just create your interface in the wrapper class and test against that?

In terms of needing to set up users etc, I think that would be a bullet you need to bite. It would seem odd to mock a wrapper PInvoke call, since you would simply just confirm and interface exists :)

于 2008-09-10T05:14:38.240 に答える