how to assert for any IOException in mstest unit testing c#

Posted on 2014-08-03
Last Modified: 2014-08-03
I have a unit test that is testing that an IOException is thrown if a file is not found.  I set the test up as follows:

        public void Throws_Error_When_XMLFileName_Is_Not_Found()
            // Arrange
            var mockHttpContextBase = new Mock<HttpContextBase>();
            var mockHttpServerUtilityBase = new Mock<HttpServerUtilityBase>();
            mockHttpContextBase.Setup(x => x.Server).Returns(mockHttpServerUtilityBase.Object);
            mockHttpServerUtilityBase.Setup(server => server.MapPath(It.IsAny<string>())).Returns(@"C:\SomeDirectory\SomeFile.XML");

            // Act
            LoadServerEnvironmentsFromXML sut = new LoadServerEnvironmentsFromXML(mockHttpContextBase.Object);

            // Assert
            // An exception should be thrown and test will fail if it doesn't

Open in new window

I don't care if it throws a DirectoryNotFoundException or FileNotFoundException.  How do I test for that?  Right now the test fails because it says a DirectoryNotFoundException was thrown.

Thanks for any suggestions!
Question by:dyarosh
    LVL 74

    Accepted Solution

    Usually when you write unit tests you write one test for each condition you anticipate having. In other words, you would write one test for each kind of exception you expect:  since each exception is generated under different circumstances, you really have different paths through your code, and each path should be tested. However, based on your question you could do something like:

    public void TestMethod1()
            // your test code
        catch (Exception ex)
            Assert.IsTrue(ex is IOException ||
                          ex is DirectoryNotFoundException ||
                          ex is FileNotFoundException);

    Open in new window


    Author Closing Comment

    Thank you.  I was hoping there was a way to catch a general IO Exception but I will use your solution.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video discusses moving either the default database or any database to a new volume.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now