In this particular example I was working with the WSUS API provided by Microsoft to interact with WSUS. I specifically was trying to save a new signing certificate, but I obviously didn't want to actually do that on a WSUS server. The solution was to use shims and stubs this time.
1: private string _fileNameParameter = "empty";
2: private SecureString _passwordParameter = new SecureString();
1: private StubIUpdateServer PrepareIUpdateServerStub()
2: {
3: FakesDelegates.Action<string, SecureString> setSigningCertificateAction = (fileName, password) =>
4: {
5: _fileNameParameter = fileName;
6: _passwordParameter = password;
7: };
8:
9: var iUpdateServerConfiguration = new StubIUpdateServerConfiguration
10: {
11: SetSigningCertificateStringSecureString = setSigningCertificateAction
12: };
13:
14: var iUpdateServerStub = new StubIUpdateServer
15: {
16: GetConfiguration = () => iUpdateServerConfiguration
17: };
18:
19: return iUpdateServerStub;
20: }
1: [TestMethod]
2: public void SetSigningCertificateShouldPassParametersToWsusApiAndReturnTrue()
3: {
4: using (ShimsContext.Create())
5: {
6: // arrange
7: const string expectedFileName = "fileName";
8: const string expectedPasswordString = "password";
9: var expectedPassword = new SecureString();
10: foreach (var c in expectedPasswordString)
11: {
12: expectedPassword.AppendChar(c);
13: }
14:
15: ShimAdminProxy.GetUpdateServerStringBooleanInt32 = (name, isSsl, port) => PrepareIUpdateServerStub();
16:
17: var wsusRepository = new WsusRepository();
18:
19: // act
20: var result = wsusRepository.SetSigningCertificate(expectedFileName, expectedPassword);
21:
22: // assert
23: Assert.AreEqual(expectedFileName, _fileNameParameter);
24: Assert.AreEqual(expectedPassword.ToString(), _passwordParameter.ToString());
25: Assert.IsTrue(result);
26: }
27: }
I broke out a bunch of the stub creation stuff so I could reuse it across multiple tests. That's what the PrepareIUpdateServerStub method is for. That's also why I have the global variables _fileNameParameter and _passwordParameter. The method I was testing is essentially a pass-through to the WSUS API that we reuse in multiple applications so the method itself was fairly straightforward (check if a file and password were passed, then pass them on to WSUS).
Like I said at the beginning, working with Shims, Fakes, and Stubs is always a challenge for me so hopefully this documentation will help me next time I need to do it.
No comments:
Post a Comment