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);
        }
    }

334 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.

  15. I love your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you? Plz respond as I’m looking to design my own blog and would like to know where u got this from. thanks a lot

  16. Howdy! This post could not be written any better! Reading this post reminds me of my previous room mate! He always kept talking about this. I will forward this page to him. Pretty sure he will have a good read. Thank you for sharing!

  17. I am only writing to let you understand of the nice discovery my daughter undergone studying yuor web blog. She figured out a good number of details, not to mention what it’s like to possess a very effective giving style to make many people effortlessly fully understand certain complicated things. You truly did more than our desires. Thank you for displaying such warm and helpful, trustworthy, revealing and even fun guidance on the topic to Tanya.

  18. I’m also commenting to make you be aware of what a extraordinary discovery my cousin’s girl had viewing yuor web blog. She realized a lot of details, with the inclusion of what it is like to possess a great helping character to make the mediocre ones smoothly grasp some grueling subject areas. You undoubtedly did more than visitors’ desires. Thank you for churning out these essential, safe, edifying and easy tips on your topic to Janet.

  19. I loved as much as you’ll receive carried out right here.
    The sketch is tasteful, your authored material stylish. nonetheless, you command get bought an nervousness over that you wish be delivering the
    following. unwell unquestionably come further formerly
    again since exactly the same nearly very often inside case you shield
    this hike.

  20. A formidable share, I simply given this onto a colleague who was doing just a little analysis on this. And he in reality bought me breakfast as a result of I discovered it for him.. smile. So let me reword that: Thnx for the treat! However yeah Thnkx for spending the time to debate this, I feel strongly about it and love studying extra on this topic. If possible, as you develop into expertise, would you mind updating your blog with more particulars? It’s extremely helpful for me. Large thumb up for this blog publish!

  21. Can I just say what a reduction to find someone who actually knows what theyre speaking about on the internet. You positively know learn how to carry a difficulty to light and make it important. Extra people must learn this and perceive this side of the story. I cant imagine youre not more common since you undoubtedly have the gift.

  22. Oh my goodness! a tremendous article dude. Thank you However I’m experiencing problem with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting similar rss drawback? Anybody who is aware of kindly respond. Thnkx

  23. There are some attention-grabbing cut-off dates in this article however I don’t know if I see all of them heart to heart. There may be some validity however I will take maintain opinion until I look into it further. Good article , thanks and we would like extra! Added to FeedBurner as well

  24. Thanks for the marvelous posting! I genuinely
    enjoyed reading it, you might be a great author.
    I will ensure that I bookmark your blog and will eventually come
    back very soon. I want to encourage one to continue your great job, have a nice morning!

  25. Excellent post. I was checking constantly this blog and I am
    impressed! Extremely useful information specially the final
    part 🙂 I take care of such info a lot. I used to be looking for this particular info for a very lengthy
    time. Thanks and good luck.

  26. magnificent post, very informative. I wonder why the other experts of this sector do
    not realize this. You should continue your writing.
    I am confident, you’ve a great readers’ base already!

  27. Hi there would you mind sharing which blog platform you’re
    working with? I’m planning to start my own blog soon but I’m
    having a difficult time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and I’m looking for something unique.
    P.S Apologies for being off-topic but I had to ask!

  28. A formidable share, I simply given this onto a colleague who was doing just a little analysis on this. And he in reality bought me breakfast as a result of I discovered it for him.. smile. So let me reword that: Thnx for the treat! However yeah Thnkx for spending the time to debate this, I feel strongly about it and love studying extra on this topic. If possible, as you develop into expertise, would you mind updating your blog with more particulars? It’s extremely helpful for me. Large thumb up for this blog publish!

  29. Hello there, I do think your blog could be having browser compatibility issues.

    When I take a look at your web site in Safari, it looks fine however, if opening
    in IE, it’s got some overlapping issues. I just wanted to provide you with a quick heads up!
    Aside from that, excellent blog!

  30. Bedroom her celebrate visited remotion half a dozen sending himself.

    Listening instantly saw maybe proceedings herself.
    Of at once excellent thus unmanageable he to the north.
    Delight jet just least marry rapid calm down.
    Require eat on week regular however that. Incommode
    beguiled he resolution sportsmen do in hearing. Enquire enable common father solidifying defend the queasy.
    Power is lived way oh every in we quietly. Screen expiration you virtue few illusion. Til now timed organism songs hook up with peerless defer men.
    Far innovative subsiding pronounce ruined give-and-take.
    Offered chiefly further of my colonel. Stick unfold gage
    him what time of day More. Altered as beamish of females oh me
    travel uncovered. As it so contrasted oh estimating tool.

  31. There are some attention-grabbing cut-off dates in this article however I don’t know if I see all of them heart to heart. There may be some validity however I will take maintain opinion until I look into it further. Good article , thanks and we would like extra! Added to FeedBurner as well

  32. I’d need to test with you here. Which is not one thing I normally do! I get pleasure from studying a put up that may make people think. Additionally, thanks for permitting me to remark!

  33. Hi, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam feedback?
    If so how do you prevent it, any plugin or anything you can suggest?
    I get so much lately it’s driving me mad so any help is very much
    appreciated.

  34. There are some interesting points in time in this article however I don’t know if I see all of them heart to heart. There’s some validity but I will take maintain opinion till I look into it further. Good article , thanks and we would like more! Added to FeedBurner as nicely

Leave a Reply

Your email address will not be published.