chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I am currently testing code that implements a custom configuration section handler. All validation takes place when the configuration node is read, so my tests will incorporate many different values for attributes on the XmlNode (this node is usually supplied by the .NET Framework in a call to IConfigurationSectionHandler.Create())

I would like to mock an instance of XmlNode to return the wide array of values that I will test. I tried
MockObject configXmlMock = MockManager.MockObject(typeof(XmlNode));

but I received this exception:
TestCase 'MyTestHarness.Load' failed: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> System.MethodAccessException : System.Xml.XmlNode..ctor()
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at TypeMock.s.b(Type A_0)
at TypeMock.MockManager.MockObject(Type type, Constructor mockConstructors, Object[] args)
at TypeMock.MockManager.MockObject(Type type, Object[] args)
MyTestHarness.Load()
--TargetInvocationException
at MockXmlNode..ctor()

Using Reflector, I opened up System.Xml.dll and noticed that the XmlNode constructor is marked internal. I'm guessing that I cannot even access the constructor, and this is my problem.

Am I SOL?
asked by bwatts (600 points)

1 Answer

0 votes
Hi
I'm guessing that I cannot even access the constructor, and this is my problem.


This is the same problem as in the last response in previous topic

This feature will added in a future version. To workaround you must create the real node.
// mock next XmlAttribute
Mock configXmlMock = MockManager.Mock(typeof(XmlAttribute));
// args for XmlAttribute
object[] args = new object[] { "prefix", "localName", "namespace", 
     xmlDoc};
// create the XmlNode
XmlNode mockedXmlNode =Activator.CreateInstance(typeof(XmlAttribute),
 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
 null,args,null) as XmlNode;
answered by scott (32k points)
...