如何模拟Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.CryptographyManager

时间:2018-10-31 18:10:14

标签: c# unit-testing .net-core moq .net-standard

我正在使用dotnet核心Standard将一个小的NuGet包放在一起。

NuGet将解析一个加密的SAML包。

我们的组织使用Microsoft.Practices.EnterpriseLibrary.Security.Cryptography库。

为了允许分离,我编写了代码以在其中一个类的构造函数中接受CryptographyManager的实例。

我正在尝试编写单元测试,以测试加密字符串的解密,但不知道如何Moq CryptographyManager。

单元测试项目是一个DotNet Core项目。

我具体是在NuGet中打电话:

var eb = Convert.FromBase64String(_xmlString);
// Decryption occurs here
var db = _cryptographyManager.DecryptSymmetric("RijndaelManaged", eb);
_xmlString = Encoding.Unicode.GetString(db);

有人可以提供有关如何进行单元测试的指示吗?我会提供一些代码,尽管不知道从哪里开始……我的单元测试缺少很大一部分:

    [TestMethod]
    public void TestThatEmployeeInformationEncryptedIsParsedCorrect()
    {

        // arrange
        // NO IDEA WHAT TO DO HERE //
        CryptographyManager cryptographyManager = null;   

        EmployeeInformation expected = new EmployeeInformation
        {
            FirstName = "Test",
            LastName = "Case",
            EmployeeNumber = "0001111111",
            LanguageCode = "en",
            CountryCode = "CA",
            RequestedPage = string.Empty,
            PositionId = "POS9999999",
            Role = "EKR"
        };

        IParser p = new XMLParser(_encryptedGoodXml, cryptographyManager);

        // act
        EmployeeInformation result = p.EmployeeInformation;

        // assert
        result.Should().BeEquivalentTo(expected);
    }

1 个答案:

答案 0 :(得分:4)

正如我从文档中看到的,inlined@Test fun `safeSet - is synchronized`() { val insideSet = AtomicInteger() val threadsRun = CountDownLatch(2) val t1 = Thread({ threadsRun.countDown() sut.safeSet { currentValue: Int -> insideSet.incrementAndGet() try { Thread.sleep(100000) } catch (interrupted: InterruptedException) { BoseLog.debug(interrupted) } currentValue + 1 } }) t1.start() val t2 = Thread({ threadsRun.countDown() sut.safeSet { currentValue: Int -> insideSet.incrementAndGet() try { Thread.sleep(100000) } catch (interrupted: InterruptedException) { BoseLog.debug(interrupted) } currentValue + 2 } }) t2.start() threadsRun.await() Thread.sleep(100) assertEquals(1, insideSet.get()) t1.interrupt() t2.interrupt() Thread.sleep(100) assertEquals(2, insideSet.get()) } 类,因此可以很容易地对其进行模拟,例如:

CryptographyManager

在此之后,您必须设置您的实际代码对其进行的调用:

abstract

然后您就可以使用模拟了:

var mockCryptoManager = new Mock<CryptographyManager>();