Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is unit testing the definition of an interface necessary?

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?

like image 271
moribvndvs Avatar asked Mar 16 '10 22:03

moribvndvs


People also ask

Can we write unit test for interface?

You can't. It has no implementation. You do want to test each and every class that implements this interface. To check that any class that implements the interface meets the expectations of the clients of that interface.

How interface is useful in unit testing?

With an interface, you can create a simple mock class which just implements the few methods you need. Most mocking frameworks have built-in support for this. "Implement" here usually means "return a fixed value". This way, you can quickly build the environment that the class under test needs.

How do you test an interface in unit testing?

To test an interface with common tests regardless of implementation, you can use an abstract test case, and then create concrete instances of the test case for each implementation of the interface.

Is unit testing always necessary?

Unit tests are also especially useful when it comes to refactoring or re-writing a piece a code. If you have good unit tests coverage, you can refactor with confidence. Without unit tests, it is often hard to ensure the you didn't break anything.


1 Answers

In my opinion, just testing the interface using a mocking framework tests little else than the mocking framework itself. Nothing I would spend time on, personally.

I would say that what should drive the design of the interface is what functionality that is needed. I think it would be hard to identify that using only a mocking framework. By creating a concrete implementation of the interface, what is needed or not will become more obvious.

The way I tend to do it (which I by no means claim is the recommended way, just my way), is to write unit tests on concrete types, and introduce interfaces where needed for dependency injection purposes.

For instance, if the concrete type under test needs access to some data layer, I will create an interface for this data layer, create a mock implementation for the interface (or use a mocking framework), inject the mock implementation and run the tests. In this case the interface serves no purpose than offering an abstraction for the data layer.

like image 85
Fredrik Mörk Avatar answered Oct 15 '22 10:10

Fredrik Mörk