System.DirectoryServices.AccountManagement.UserPrincipal

1,380 views
Skip to first unread message

brembos

unread,
Mar 23, 2011, 7:57:55 AM3/23/11
to Moq Discussions
Hi. I have a problem with Moq and the .Net
System.DirectoryServices.AccountManagement.UserPrincipal.

Both
var stub = new Mock<UserPrincipal>();
var principal = stub.Object;
and
var stub = new
Mock<PrincipalContext>(ContextType.Machine);
var principalContext = stub.Object;

Gives me Object reference not set to an instance of an object when
accessing stub.Object.

This is the only time I've had this problem. Other classes and
interfaces works just fine. What is going on?

Daniel Cazzulino

unread,
Mar 23, 2011, 9:34:39 AM3/23/11
to moq...@googlegroups.com
First thing I'd look is at what is System.DirectoryServices.AccountManagement.UserPrincipal doing in its constructor.
When you mock a concrete class, you're essentially inheriting from it, so its constructor runs too as well as any other initialization in there. 

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471


brembos

unread,
Mar 24, 2011, 4:21:50 AM3/24/11
to Moq Discussions
Tryed using Reflector to look inside UserPrincipal, which is part of
the framework, didn't find anything. But I guess you are on the right
track.

I've also tried to make my own class, inheriting from UserPrincipal:
public class UserPrincipalStub : UserPrincipal. Which gets me a bit
further, but gives an key not found exception trying to set one of the
properties.

Daniel Cazzulino

unread,
Mar 24, 2011, 7:17:21 AM3/24/11
to moq...@googlegroups.com

Create your own IUserPrincipal instead?


--

Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com

brembos

unread,
Mar 24, 2011, 7:21:30 AM3/24/11
to Moq Discussions
Well, the problem is that this class is part of the Framework. How can
I make use of an interface in this case?

Daniel Cazzulino

unread,
Mar 24, 2011, 10:46:06 AM3/24/11
to moq...@googlegroups.com
wrap it?



/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471


On Thu, Mar 24, 2011 at 08:21, brembos <st...@brembo.name> wrote:
Well, the problem is that this class is part of the Framework. How can
I make use of an interface in this case?

brembos

unread,
Mar 25, 2011, 4:31:20 AM3/25/11
to Moq Discussions
Yes, that seems to be the solution. A bit tedious, but probably the
only way to go.
Reply all
Reply to author
Forward
0 new messages