1

I'm using NUnit and Rhino Mocks. I use the AAA-syntax and I do the Arrange and Act in the setup method, and every Test method is an Assert.

[TestFixture] public class When_subSystem_throws_exception { SomeClass subject; // System under test [SetUp] public void Setup() { // Arrange IDependency dependency = MockRepository.GenerateStub<IDependency>(); dependency.Stub(m => m.DoStuff()).Throw(new Exception()); // This method is called from within SomeMethod() subject = new SomeClass(dependency); // Act subject.SomeMethod("Invalid Input"); } // Assert [Test] public void should_log_an_exception_to_the_logger() { // Do stuff to verify that an exception has been logged } // More tests } 

As you might expect, the code in SomeMethod() throws an exception (as expected), wich makes every test fail (unwanted). I workaround this by doing

try { // Act subject.SomeMethod("Invalid Input"); } catch(Exception ex) { // Swallow, this exception is expected. } 

But that is just ugly.

What I would like to be able to do is

[SetUp] [ExpectedException] // <-- this works for Test methods, but not for SetUp methods public void Setup() { // etc... } 

but I can't find anything like it.

Do you know of anything?

3 Answers 3

2

I don't think using an attribute like ExpectedException is a good idea. SetUp is to prepare something for the test methods, it shouldn't throw exception. If it must throw, and you want to limit the code line number. Then put them into one line like below:

try { subject.SomeMethod("Invalid Input"); }catch { } 
Sign up to request clarification or add additional context in comments.

5 Comments

How can you assure that the exception has been throw?
@Simone sorry I don't understand what you say.
If you want to test whether the exception is thrown or not, how can you check this with such a code? if it's thrown, you do nothing different than if it's not thrown.
+1 For simply making it a one-liner (When I get the rep xD). Didn't think of that.
@Simone, I don't want to test wether the exception is thrown or not, I make sure it is thrown somewhere in that method using mocks (I see that is not clear from my question, will update it). What I want to test is the stuff that happens When an exception is thrown (like logging).
2

It doesn't work in Setup for a reason, not because of NUnit's bug.

It's a very bad practice for a unit-test to have exception throwing inside the SetUp method. If you are testing a particular scenario where a exception is the expected result, it should be done inside a [Test] method. You should rearrange your code subsequently.

1 Comment

I'm not sure why that would be bad practise. As I do my "Acts" in the SetUp (that's a given for me), expected exceptions that are thrown in those "Acts" are naturally also inside the SetUp. I'm just looking for a more elegant solution than try-catch.
1

Your "act" step should be in the test method not the setup.

The setup is for setting up pre-requisite conditions and common objects for the test(s) - i.e. common or repeated "arrange" steps.

Each test method should "act" and "assert" individually (and may also need additional "arrange" steps specific to the test).

2 Comments

Hmm, I guess how I unit test isn't really standard is it? I think of it as "NUnit meets BDD" and it works for me. This is the first time I've had a "problem" with it, and it's solved through a simple try-catch. What I'm looking for is a more elegant solution than try-catch. The "act" is staying in the SetUp :p.
then go with try-catch, it's the more "elegant" solution (and your only option).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.