Rhino Mocks 嘲笑 WindowsImpersonationContext
是否可以使用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);
}
Is it possible to use Rhino Mocks to mock WindowsImpersonationContext?
I get:
System.MissingMethodException : Can't find a constructor with matching arguments
----> System.MissingMethodException : Constructor on type 'WindowsImpersonationContextProxy04bee852de914d5b8a47d6776edc4cb3'
var windowsImpersonationContext = mockRepository.Stub<WindowsImpersonationContext>();
mockImpersonation.Stub(x => x.ImpersonateUser("username", "domain", "password")).Return(windowsImpersonationContext);
Here is my code I need to mock
public interface IImpersonation
{
WindowsImpersonationContext ImpersonateUser(string sUsername, string sDomain, string sPassword);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您想要存根 IImpersonation,而不是 WindowsImpersonationContext。这就是 IImpersonation.ImpersonateUser 返回的内容。
但是,WindowsImpersonationContext 没有公共构造函数,因此您无法创建模拟构造函数来进行测试。您可能想要为 WindowsImpersonationContext 创建一个接口。存根接口以进行测试和生产,创建一个包装类来实现该接口并将调用委托给真正的 WindowsImpersonationContext。
Looks like you want to stub IImpersonation, not WindowsImpersonationContext. That is what is returned by IImpersonation.ImpersonateUser.
However, WindowsImpersonationContext doesn't have a public constructor, so you can't create a mock one for testing. You may want to create an interface for the WindowsImpersonationContext. Stub the interface for testing and for production, create a wrapper class the implements the interface and delegates the calls to the real WindowsImpersonationContext.
您不能存根/模拟 WindowsImpersonationContext,因为它是一个具体类。我不认为你可以自己创建它(抱歉,我没有方便检查的 VS),所以我建议更改你的接口以从 WindowsImpersonationContext 返回你需要的任何内容(可能包装在你自己的类型中),这样你可以嘲笑那个。
You can't stub/mock WindowsImpersonationContext because it is a concrete class. I don't think you can create it yourself (sorry, I don't have VS handy to check), so I would suggest changing your interface to return whatever you need from WindowsImpersonationContext (possibly wrapped in your own type), so that you can mock that.