chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
+1 vote
I cannot get the below test to pass:

        
        [Isolated]
        [TestMethod]
        public void UseSymCryptographyToSetAKey()
        {
            _symCrypt = new SymCryptography();
            Isolate.WhenCalled(() => _symCrypt.Encrypt("Test")).WillReturn("jljljl");
            Isolate.WhenCalled(() => _symCrypt.Key = "key").CallOriginal();
            Isolate.WhenCalled(() => _symCrypt.Key).CallOriginal();
            _adapter = new CryptographyAdapter(_symCrypt);

            _adapter.SetKey("key");
            var encrypted = _adapter.Encrypt("Test");

            try
            {
                Assert.IsFalse(encrypted == "Test");
                Isolate.Verify.WasCalledWithAnyArguments(() => _symCrypt.Key);
            }
            catch (System.Exception e)
            {
                Assert.Fail(e.ToString());
            }
        }


In the method call _adapter.Encrypt I have the following code:

        public override string Encrypt(string value)
        {
            _symCrypt.Key = Key;
            return _symCrypt.Encrypt(value);
        }


Whatever I do, (also tried mocking up the SymCryptography) I get the error:

Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException was unhandled by user code
Message=Assert.Fail failed. TypeMock.VerifyException:
TypeMock Verification: Method Informedica.SecureSettings.Cryptographers.SymCryptography.Key was expected but was not called
at em.a(ji A_0, d2 A_1, ho A_2, Predicate`1 A_3)
at b7.b(Delegate A_0)
at b7.WasCalledWithAnyArguments[T](Func`1 func)
at Informedica.SecureSettings.Tests.CryptographyAdapterShould.UseSymCryptographyToSetAKey() in C:\Development\SecureSettings\Informedica.SecureSettings.Tests\CryptographyAdapterShould.cs:line 29
Source=Microsoft.VisualStudio.QualityTools.UnitTestFramework
StackTrace:
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.HandleFail(String assertionName, String message, Object[] parameters)
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Fail(String message, Object[] parameters)
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Fail(String message)
at Informedica.SecureSettings.Tests.CryptographyAdapterShould.UseSymCryptographyToSetAKey() in C:\Development\SecureSettings\Informedica.SecureSettings.Tests\CryptographyAdapterShould.cs:line 33
InnerException:

But I do call SymCryptography.Key, I checked that in the debugger. However when I add Key = _symCrypt.Key to th Encrypt method, the test passes!!??

So, why can I not just check the property set method?
asked by halcwb (5.5k points)

2 Answers

+1 vote
Hi

It seems like the problem is that inside Encrypt you are calling _symCrypt.Key setter while in the test you verifying the call to _symCrypt.Key getter. Try changing the vitrification code to:
Isolate.Verify.WasCalledWithAnyArguments(() => _symCrypt.Key = null);


Please let me know if it helps.
answered by ohad (35.5k points)
0 votes
Oops, not so clever of me, you were completely right, works fine now. Thanks.
answered by halcwb (5.5k points)
...