4

It seems that nUnit have removed the ExpectedException attribute in nUnit 3. So my question is: how can I catch and, therefore, test that my constructor throws an ArgumentOutOfRangeException when I past invalid parameters to my class?

public Year(int value)
{
    if (value < 1) throw new ArgumentOutOfRangeException(nameof(value), "Cannot be less than 1.");
    if (value > 9999) throw new ArgumentOutOfRangeException(nameof(value), "Cannot be greater than 9999.");
    Value = value;
    AddMonths();
}

My test:

[Test]
public void ShouldNotBeAbleToCreateYear()
{
    // Arrange...
    const int yearValue = 0;

    // Act...
    var year = new Year(yearValue);

    // Assert...
    // Need to test that an ArgumentOutOfRangeException was thrown!?
}

I have checked nUnits documentation and the answer to this eludes me!

Many thanks in advance...

  • It's a lot faster to search SO or google for something. `NUnit3 ExpectedException` returned those duplicates in the very first result. This is also explained in NUnit 3's documentation. NUnit 3 [replaced attributes with Constraints for assertions](https://github.com/nunit/docs/wiki/Assertions). You'll find a list of built-in constraints [here](https://github.com/nunit/docs/wiki/Constraints) – Panagiotis Kanavos Jul 26 '18 at 10:06
  • The relevant constraints are [ThrowsConstraint](https://github.com/nunit/docs/wiki/ThrowsConstraint) and [ThrowsNothingConstraint](https://github.com/nunit/docs/wiki/ThrowsNothingConstraint) – Panagiotis Kanavos Jul 26 '18 at 10:10
  • I searched SO and Googled, and I find your marking as duplicated, more than a little patronising! :( – John Morsley Jul 26 '18 at 11:02
  • You asked for something that's already answered with some *very good answers*. Posting another question can be considered *disrespectful* because it makes it *harder* for people with similar problems to find a good answer. It's also disrespectful to the people that *answer* questions and have to find the duplicates and the links to the documentation. – Panagiotis Kanavos Jul 26 '18 at 11:33
  • check for example [this answer](https://stackoverflow.com/a/42780980/134204). Not only how to use Assert.Throws, but how to combine it with local functions. Existing good questions *accumulate good answers* – Panagiotis Kanavos Jul 26 '18 at 11:35
  • 4
    I nominated this is being Reopened. We should be more understanding of the questions being asked here. None of those Duplicates shows how to do this with the Constructor. So I find it to be a different question and not obviously to a Junior Developer that they can just construct the class in the Assert.Throws(). – Andrew T Finnell Jul 26 '18 at 12:35
  • 2
    @JohnMorsley The duplicates do show you how to do it, even though it's not obvious. Just use Assert.Throws(() => new Year(-1)); – Andrew T Finnell Jul 26 '18 at 12:37
  • That's what I was looking for @AndrewTFinnell! Thanks very much mate, that works like a dream. And I concur, I'm not a novice, but new to the nUnit framework version 3. Thanks again for your time... – John Morsley Jul 26 '18 at 14:22

0 Answers0