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 need to do an ExpectAndThrow on XmlNode, but it simply will not work. I am trying to expect an InvalidCastException.

My code:

            MockManager.Init();

            Mock mockXmlNode = MockManager.Mock(typeof(XmlNode));

            mockXmlNode.ExpectAndThrow("SelectSingleNode", new InvalidCastException());

...
...

            MockManager.Verify();

            MockManager.ClearAll();


This is what I get for my trouble:

Test method
RoadLoans.PresenterTest.ApplicationControllerConfigurationSectionHandlerTest.ApplicationControllerConfigurationSectionHandlerCreateTestInvalidCastException threw exception TypeMock.VerifyException, but exception System.InvalidCastException was expected. Exception message: TypeMock.VerifyException:
TypeMock Verification: Expected a new instance of System.Xml.XmlNode (try using MockAll)


Yes I have tried MockAll.

Any ideas?
asked by csustek (4k points)

3 Answers

0 votes
Hi
Its hard to know whats going on from the code you posted. :wink:
Can you please post the rest of the test code?
answered by ohad (35.4k points)
0 votes
Here is the code I'm trying to unit test:

        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            try
            {
                _controllers = section.SelectSingleNode("applicationControllers");
            }
            catch (InvalidCastException ex)
            {
                bool rethrow = ExceptionHandler.HandleException(ex, ExceptionPolicies.Presenter.ToString());
                if (rethrow)
                    throw;
            }
            catch (System.Xml.XPath.XPathException ex)
            {
                bool rethrow = ExceptionHandler.HandleException(ex, ExceptionPolicies.Presenter.ToString());
                if (rethrow)
                    throw;
            }
            return this;
        }


Here is the entire unit test:
        [TestMethod]
        [ExpectedException(typeof(InvalidCastException))]
        public void ApplicationControllerConfigurationSectionHandlerCreateTestInvalidCastException()
        {
            MockManager.Init();

            Mock mockXmlNode = MockManager.Mock(typeof(XmlNode));
            mockXmlNode.ExpectAndThrow("SelectSingleNode", new InvalidCastException());

            Mock factoryMockLibrary = MockManager.Mock(typeof(ExceptionHandler));
            factoryMockLibrary.ExpectAndReturn("HandleException", true);

            ApplicationControllerConfigurationSectionHandler configuration = (ApplicationControllerConfigurationSectionHandler)ConfigurationManager.GetSection("controllerSection/controllerHelper");
            MockManager.Verify();
            MockManager.ClearAll();
        }
answered by csustek (4k points)
0 votes
Hi
The Verify exception happens because TypeMock did not detect instance creation of XmlNode (ie there is no new XmlNode() after MockManager.Mock(typeof(XmlNode)); ).

You can check this by using TypeMock Tracer.
To run the Tracer open:
START -> Programs -> TypeMock.NET -> Tracer
Than run your test.
If you need help in understanding the Tracer
you can save the Tracer output from the File -> Save menu and send it to me.
I'll send you my address offline.
answered by ohad (35.4k points)
...