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

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:

        [TestMethod]
        [ExpectedException(typeof(IOException))]
        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);
            sut.LoadServerList("SomeFile.xml");

            // 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!
dyaroshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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:

[TestMethod]
public void TestMethod1()
{
    try
    {
        // your test code
    }
    catch (Exception ex)
    {
        Assert.IsTrue(ex is IOException ||
                      ex is DirectoryNotFoundException ||
                      ex is FileNotFoundException);
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dyaroshAuthor Commented:
Thank you.  I was hoping there was a way to catch a general IO Exception but I will use your solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.