Is unit testing the definition of an interface necessary?

Posted by HackedByChinese on Stack Overflow See other posts from Stack Overflow or by HackedByChinese
Published on 2010-03-16T22:36:44Z Indexed on 2010/03/16 22:41 UTC
Read the original article Hit count: 336

Filed under:
|
|
|

I have occasionally heard or read about people asserting their interfaces in a unit test. I don't mean mocking an interface for use in another type's test, but specifically creating a test to accompany the interface.

Consider this ultra-lame and off-the-cuff example:

public interface IDoSomething
{
   string DoSomething();
}

and the test:

[TestFixture]
public class IDoSomethingTests
{
   [Test]
   public void DoSomething_Should_Return_Value()
   {
        var mock = new Mock<IDoSomething>();
        var actualValue = mock.Expect(m => m.DoSomething()).Returns("value");

        mock.Object.DoSomething();
        mock.Verify(m => DoSomething());
        Assert.AreEqual("value", actualValue);
   }
}

I suppose the idea is to use the test to drive the design of the interface and also to provide guidance for implementors on what's expected so they can draw good tests of their own.

Is this a common (recommended) practice?

© Stack Overflow or respective owner

Related posts about unit-testing

Related posts about interfaces