[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2014-08-03
Medium Priority
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 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 40237869
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

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

872 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