1

Possible Duplicate:
Unit testing void methods?

I need to write unit test cases for the GUI methods which does not return any value, how i can test the ac

Community
  • 1
  • 1
user400408
  • 11
  • 1
  • 2

3 Answers3

3

You can make sure it doesn't throw, at least by

Assert.DoesNotThrow<ExceptionType>( () => myClass.myMethod() );

However, bear in mind that a method that does not return a value, but does something, relies on side effects and therefore is not really amenable to unit testing, as you cannot test the whole state of a system in a unit test.

Aidan
  • 4,783
  • 5
  • 34
  • 58
  • If a test throws an exception then it does not pass, why would you test if it does not throw an exception unless you wanted to narrow it down to a specific exception type? – Ben Robinson Apr 28 '11 at 12:13
  • Mainly to make it more explicit what you are testing for. One function of unit tests is to make sure that the code does what you want. Another function is to communicate what the method does and how to call it. Tests should give some sense of what you expect to happen, and using a specific test like DoesNotThrow can make the meaning of the function more explicit. – Aidan Apr 28 '11 at 15:46
  • It is implicit in a test that you expect it to pass, if you have to be explicit in this then you are trying to comunicate information to imbeciles. – Ben Robinson May 03 '11 at 15:49
  • 1
    Actually, mostly I'm trying to communicate with myself for when I return to the code months later. If that's because I'm an imbecile, so be it. As well as testing the correctness of the method, a unit test provides an example of how the code should be used. – Aidan May 04 '11 at 19:09
2

You can mock certain methods and classes that your method calls and assert that they are called. You can use some mocking framework for this purpose, most of them have the functionality to test: Assert.IsCalled();

You can also assert that the certain changes in environment that are expected are met. For example, a file is created.

anthares
  • 11,070
  • 4
  • 41
  • 61
1

If it does not throw an exception when passed correct values for params (if any), then it passes. If it throws the correct excpeptions when passed invalid values then it passes.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79