views:

56

answers:

1

There are two testing scenarios I am unclear on. Both appear at first sight to create very brittle tests.

First, when should one unit test the interface (i.e. verify that an interface has a set signature)?

Second, when should one "test the sequence diagram" (a term I just made up) - meaning verifying calls are being made to the appropriate objects?

+2  A: 

Testing the interface means that you should only test the members which are available on the public interface. In other words, don't test private stuff. Take the unit under test as a black-box. This makes the tests more maintainable because you can change the implementation details without braking the tests. Tests will also express what your unit under test is good for, not how it is implemented.

Testing calls made on other objects is called "interaction test" (not to be confused with integration tests, where you don't mock the other objects). Interaction tests are needed when your unit under test calls a method on another object without depending on it. I try to explain it with an example.

Following method needs to be tested:

public decimal CalculateTax(Order order);

Lets assume that this method needs to call

TaxRules TaxRuleProvider.GetRules(Country country)

which returns some local rules. When it doesn't call it, it will not be able to return the correct result. It will miss vital information. You don't need to test if it had been called, just test the result.

Another method:

public void StoreThingy(Thingy toBeStored);

It will call

public void NotificationBroker.NotifyChanges(SomeChanges x);

StoreThingy doesn't depend on the notification. You can't decide on its interface if it sent notifications or not. You need to test this by an interaction test.

Typically the methods for interaction tests return void. In this category are all kinds of events and notifications and methods like Commit().

Stefan Steinegger
Great answer. I wasn't clear on "interaction tests", thanks. Please clarify your first line "you should not test if the interface is complete".
Ben Aston
@Ben: "if the interface is complete" is how I understood your question cit. "verify that an interface has a set signature". Did I misunderstand?
Stefan Steinegger
@Stefan - oh I understand now, I was misreading the line (I was mentally inserting a comma after "you should not test" for some reason).
Ben Aston
I rephrase it...
Stefan Steinegger
One further thought. "don't test private stuff" - if you are following TDD, does this mean that you throw away tests you develop as you code? Or does TDD simply prescribe creation of tests as you describe above?
Ben Aston
@Ben: you usually don't throw away tests. Yes, TDD means to write tests on the public interface. If you feel that behind your public members is too much code to test at once, you should divide your class into several smaller classes.
Stefan Steinegger