Verifying Exceptions using IExpectException

Standard

The unit tests for NUnit contain an interesting pattern for verifying exception details. The abstract class MessageChecker in assembly nunit.framework.tests implements IExpectException, an interface introduced with NUnit 2.4. Test cases in derived test fixtures set the protected field expectedMessage to the expected exception message which can span several lines or include data from the test setup. Here is another example:

    public class FileNotFoundExceptionChecker : IExpectException
    {
        private string _expectedFileName;
    
        public FileNotFoundExceptionChecker()
        {
        }
    
        public void HandleException(Exception ex)
        {
            if (_expectedFileName == null)
                return;
    
            Assert.IsInstanceOfType (typeof (FileNotFoundException), ex);
            FileNotFoundException fileNotFoundException = (FileNotFoundException) ex;
    
            StringAssert.Contains(_expectedFileName, fileNotFoundException.Message);
            Assert.AreEqual(_expectedFileName, fileNotFoundException.FileName);
        }
    
        protected void ExpectMissingFile(string fileName)
        {
            _expectedFileName = fileName;
        }
    }
    
    [TestFixture]
    public class FileTest : FileNotFoundExceptionChecker
    {
        [Test]
        [ExpectedException(typeof(FileNotFoundException))]
        public void ReadAllBytes()
        {
            string fileName = @"C:\notexistingfile.txt";
            ExpectMissingFile(fileName);
    
            File.ReadAllBytes(fileName);
        }
    }

254 thoughts on “Verifying Exceptions using IExpectException

  1. Hello there! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?

  2. I’m now not sure the place you’re getting your info, however great topic.
    I needs to spend some time studying much more or working out more.
    Thanks for magnificent information I used to be searching for this info for
    my mission.

  3. Awesome site you have here but I was curious if you
    knew of any discussion boards that cover the same topics discussed here?

    I’d really like to be a part of community where I can get feed-back from other experienced individuals that share
    the same interest. If you have any suggestions, please let
    me know. Cheers!

  4. I have been absent for some time, but now I remember why I used to love this web site. Thank you, I’ll try and check back more frequently. How frequently you update your website?

  5. Howdy! This is kind of off topic but I need some advice from an established blog. Is it tough to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about creating my own but I’m not sure where to begin. Do you have any tips or suggestions? Thank you

  6. fantastic post, very informative. I wonder why the other experts of this sector do not notice this. You should continue your writing. I am sure, you’ve a great readers’ base already!

  7. hello there and thank you for your info – I’ve definitely picked up anything new from right here. I did however expertise some technical points using this site, as I experienced to reload the site lots of times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I’m complaining, but slow loading instances times will very frequently affect your placement in google and could damage your high quality score if advertising and marketing with Adwords. Well I’m adding this RSS to my e-mail and can look out for a lot more of your respective fascinating content. Make sure you update this again very soon..

  8. Its like you read my thoughts! You seem to understand a lot about this, like you wrote the book in it or something. I feel that you just could do with some to power the message home a bit, however instead of that, this is magnificent blog. An excellent read. I’ll certainly be back.

  9. It is truly a great and useful piece of info.
    I am glad that you just shared this useful information with us.
    Please keep us up to date like this. Thanks for sharing.

  10. Thanks for another great post. The place else may anyone get that
    kind of info in such a perfect method of writing? I have
    a presentation subsequent week, and I’m at the search for such info.

  11. hey there and thank you to your information – I’ve definitely picked up anything new from proper here. I did alternatively experience several technical points using this site, as I skilled to reload the website a lot of instances previous to I may just get it to load correctly. I had been brooding about if your web hosting is OK? No longer that I’m complaining, however sluggish loading cases times will sometimes affect your placement in google and could harm your high quality ranking if advertising and ***********|advertising|advertising|advertising and *********** with Adwords. Anyway I am including this RSS to my e-mail and can glance out for a lot more of your respective exciting content. Make sure you update this once more very soon..

  12. I spent four years trying EVERYTHING in Online Dating, and through a huge amount of trial and error, I produced a system that I will share for you. This book will take you, step by step, through everything you need to know to double, triple or even quadruple the number of women you meet online.

  13. I spent four years trying EVERYTHING in Online Dating, and through a huge amount of trial and error, I produced a system that I will share for you. This book will take you, step by step, through everything you need to know to double, triple or even quadruple the number of women you meet online.

  14. The tips you provided here are very valuable. It absolutely was such an exciting surprise to get that waiting for me immediately i woke up this very day. They are often to the point and straightforward to understand. Warm regards for the clever ideas you have shared above.

Leave a Reply

Your email address will not be published.