[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

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?
0
brettr
Asked:
brettr
  • 3
  • 3
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
philipjonathanCommented:
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
 
brettrAuthor Commented:
Leaving it blank works.  Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now