Mocking Active Directory Objects like DirectoryEntry and DirectorySearcher (MarshalByRefObject)

1,542 views
Skip to first unread message

David K.

unread,
Jan 20, 2010, 10:20:00 AM1/20/10
to Moq Discussions
I'm currently trying to mock my Active Directory interactions, but am
having some issues. I want to mock objects such as
System.DirectoryServices.DirectoryEntry and DirectorySearcher. They
both are derived from MarshalByRefObject so my understanding is that
they should be mockable. However, when I attempt to mock a
DirectoryEntry I get the error below when accessing mock.Object. Do I
need to setup the mock in a special way? Is this just not possible?
What are the suggested workarounds? Create a wrapper class?

System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. ---> System.NotSupportedException: The
'get' method is not supported on this property.
at System.Security.Permissions.EnvironmentPermissionAttribute.get_All
()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object
[] arguments, ref SignatureStruct sig, MethodAttributes
methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[]
index)
at
Castle.DynamicProxy.Generators.Emitters.CustomAttributeUtil.GetPropertyValues
(Type attType, PropertyInfo[]& properties, Attribute attribute)
at
Castle.DynamicProxy.Generators.Emitters.CustomAttributeUtil.CreateCustomAttribute
(Attribute attribute)
at
Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.DefineCustomAttribute
(Attribute attribute)
at
Castle.DynamicProxy.Generators.BaseProxyGenerator.ReplicateNonInheritableAttributes
(Type targetType, ClassEmitter emitter)
at Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateCode(Type
[] interfaces, ProxyGenerationOptions options)
at Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxy(Type
classToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyType(Type
classToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type
classToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options, Object[] constructorArguments,
IInterceptor[] interceptors)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type
classToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options, IInterceptor[] interceptors)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type
classToProxy, Type[] additionalInterfacesToProxy, IInterceptor[]
interceptors)
at Moq.Mock`1.<InitializeInstance>b__0()
at Moq.PexProtector.Invoke(Action action)
at Moq.Mock`1.InitializeInstance()
at Moq.Mock`1.get_Object()

Daniel Cazzulino

unread,
Jan 20, 2010, 10:42:04 AM1/20/10
to moq...@googlegroups.com
Moq does not mock using remoting. It uses generated classes that inherit from your target ones. 
So the same rules apply always, regardless of what it is that you're mocking: interfaces or classes behave in a consistent way.

/kzu

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


Reply all
Reply to author
Forward
0 new messages