c# - 如何模拟 Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.CryptographyManager
问题描述
我正在使用 dotnet core 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);
}
解决方案
正如我从文档中看到的那样,它CryptographyManager
是一个abstract
类,因此可以很容易地模拟它,例如:
var mockCryptoManager = new Mock<CryptographyManager>();
在此之后,您必须设置您的实际代码对其进行的调用:
mockCryptoManager
.Setup(cm => cm.DecryptSymmetric("RijndaelManaged", It.IsAny<byte[]>()))
.Returns(/* Here you have to put the value you want the mock return with */);
然后你可以使用你的模拟:
CryptographyManager cryptographyManager = mockCryptoManager.Object;
推荐阅读
- loops - NASM:将多字符输入转换为十进制
- react-native - 带有 ColdFusion 组件的 React-native axios
- c# - 如何在同一解决方案的不同winform中使用现有按钮功能
- python - 如何在 Python 中获得平衡的数据帧
- python-3.x - 从不同的行减去值
- android - 了解风格
- c++ - 在 C++ 中:如何打印小数点后的数字。
- python - 如何从后台进程向 Flask-SocketIO 发送数据
- sql - 需要在我的动态生成的经典asp站点中删除数字中的逗号
- node.js - 使用 Let's Encrypt 添加 HTTPS 后,我的 Web 应用程序出现 CORS 错误