How to pass NUnit test if no exception?

In .NET 3.5 using NUnit 2.4.8, I'm running this test:

[ExpectedException(typeof(EligibilityException))]
        [Test]
        public void OverMaxWeightSuccessTest()
        {
            new Eligibility.OverMaxWeight("501").CheckEligibility();
        }

The above throws an EligibilityException exception if the weight is over 500.  CheckEligibility() is a void.  How do I write a test for the failure condition?  Meaning, a weight of 400?

In general, I need to throw an exception if the weight is over 500.   It would be cleaner if CheckEligibility() returned a boolean.   If there a better way to do this?
brettrAsked:
Who is Participating?
 
philipjonathanConnect With a Mentor Commented:
I don't there is Assert.Pass, but probably you can just leave it alone. If you don't assert fail anything, it will be considered a pass. Either that, or you assert a "true" equals to "true"
0
 
philipjonathanCommented:
You mean you want the test to fail if CheckEligibility throws EligibilityException when weight is 400?
The simplest way is to use try-catch:

try
{
  new Eligibility.OverMaxWeight("400").CheckEligibility();
  // assert test succeeds
}
catch (EligibilityException)
{
  // assert test fails
}
0
 
brettrAuthor Commented:
No - you have it backwards.  The attribute is already taking care of the exception.  I need a way to say test failure when there isn't an exception.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
philipjonathanCommented:
I suppose you could assert a failure below the CheckEligibility line:

[ExpectedException(typeof(EligibilityException))]
[Test]
public void OverMaxWeightSuccessTest()
{
  new Eligibility.OverMaxWeight("501").CheckEligibility();
  Assert.Fail();
}

0
 
brettrAuthor Commented:
Thanks.  In your first example where you have

// assert test succeeds

what type of assert could I put there so the test passes in that case?  There isn't an Assert.Pass().   CheckEligibility() returns void so I don't have anything to check.
0
 
brettrAuthor Commented:
Leaving it blank works.  Thanks.
0
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.

All Courses

From novice to tech pro — start learning today.