I have some void methods and I need to test them, but I'm not sure about how to do it. I just know how to test methods that return something, using Assert. Someone knows how to do it? Do you guys know some links with exercices in this style?
Asked
Active
Viewed 732 times
6
-
If the methods don't do anything that you can observe externally, what useful work are they doing? – Joachim Isaksson Dec 16 '13 at 16:40
1 Answers
10
You can test two things:
- State changes after void method call (state-based testing)
- Interaction with dependencies during void method call (interaction testing)
First approach is simple (NUnit sample):
var sut = new Sut();
sut.Excercise(foo);
Assert.That(sut.State, Is.EqualTo(expectedState)); // verify sut state
Second approach requires mocks (Moq sample):
var dependencyMock = new Mock<IDependency>();
dependencyMock.Setup(d => d.Something(bar)); // setup interaction
var sut = new Sut(dependencyMock.Object);
sut.Excercise(foo);
dependencyMock.VerifyAll(); // verify sut interacted with dependency
Well, you also can test if appropriate exceptions are thrown.

Sergey Berezovskiy
- 232,247
- 41
- 429
- 459