views:

65

answers:

2

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);
}
+1  A: 

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.

Patrick Steele
A: 

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.

Grzenio