c# - Rhino Mocks 模拟 WindowsImpersonationContext

标签 c# security mocking rhino-mocks impersonation

是否可以使用 Rhino Mocks 模拟 WindowsImpersonationContext?

我得到:

System.MissingMethodException:找不到具有匹配参数的构造函数 ----> System.MissingMethodException:类型为“WindowsImpersonationContextProxy04bee852de914d5b8a47d6776edc4cb3”的构造函数

var windowsImpersonationContext = mockRepository.Stub<WindowsImpersonationContext>();
mockImpersonation.Stub(x => x.ImpersonateUser("username", "domain", "password")).Return(windowsImpersonationContext);

这是我需要模拟的代码

public interface IImpersonation
{
    WindowsImpersonationContext ImpersonateUser(string sUsername, string sDomain, string sPassword);
}

最佳答案

看起来您想要 stub IImpersonation,而不是 WindowsImpersonationContext。这就是 IImpersonation.ImpersonateUser 返回的内容。

但是,WindowsImpersonationContext 没有公共(public)构造函数,因此您无法创建模拟构造函数进行测试。您可能想要为 WindowsImpersonationContext 创建一个接口(interface)。 stub 用于测试和生产的接口(interface),创建一个实现接口(interface)并将调用委托(delegate)给真实 WindowsImpersonationContext 的包装类。

关于c# - Rhino Mocks 模拟 WindowsImpersonationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3336547/

相关文章:

C# HTML 敏捷包,从 div 中提取纯文本

C# Silverlight Datagrid - 行颜色更改

c# - ASP.NET MVC 中基于文档的安全性

security - openshift 上的 HTTP 严格传输安全

testing - 在 Elixir 中生成响应数据时避免模拟

c# - 如何使用 linq 扩展方法执行左外连接

c# - .NET 网络库

security - Symfony 2 SecurityContext 类已弃用

java - Mockito:Java - 未完成的 stub 检测

java - 我可以使用 jmock 替换工厂返回的实现吗?