我有什么可能的解决方法来使用 Moq 模拟这个对象。返回的错误消息是:“非虚拟(在 VB 中可覆盖)成员上的设置无效:p => p.Certificate.SubjectName.Name”
var st = new Mock<X509SecurityToken>();
st.SetupGet(p => p.Certificate.SubjectName.Name).Returns("test1");
var target = new X509IssuerNameRegistry("test1", "test2");
var actual = target.GetIssuerName(st.Object);
Assert.AreEqual<string>("test1", actual, "Actual security token did not equal expected security token");
注意:我正在尝试模拟X509SecurityToken。这是正在测试的类:
公共类 X509IssuerNameRegistry : IssuerNameRegistry { 只读列表 _trustedSubjectNames = new List();
public X509IssuerNameRegistry(params string[] trustedSubjectNames)
{
_trustedSubjectNames = new List<string>(trustedSubjectNames);
}
public override string GetIssuerName(SecurityToken securityToken)
{
var x509Token = securityToken as X509SecurityToken;
if (x509Token != null)
{
if (_trustedSubjectNames.Contains(x509Token.Certificate.SubjectName.Name))
{
return x509Token.Certificate.SubjectName.Name;
}
}
throw new SecurityTokenException("Untrusted issuer.");
}
}
任何工作轮次(使用起订量)还是我应该寻找另一个模拟框架来完成这个?