Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing interface makes sense?

Tags:

tdd

I'm new to TDD, but I'm not sure why should I test an interface?

Does the code below make sense??

public interface IInterface
{
    int Value { get; }
}


[TestMethod]
public void Test_iinterface_value()
{
    var iinterface = mockery.NewMock<IInterface>();
    Expect.Once.On(iinterface).GetProperty("Value").Will(Return.Value(10));
    Assert.AreEqual(iinterface.Value, 10, "Doh!");
}
like image 447
DarkVM Avatar asked May 05 '11 05:05

DarkVM


2 Answers

Unless I am mistaken it looks like you are creating a mock object that returns given value when asked for it. And then you ask for the value & you get it (surprise). I’d say it does not make sense to write such tests, unless you are testing the mocking framework itself or you expect the compiler to play some dirty tricks on you.

like image 84
zoul Avatar answered Oct 31 '22 20:10

zoul


This is known as the Mockery TDD anti-pattern.

However, you may want to write a set of tests that applies to all implementers of a given interface to ensure that the contracts is being properly implemented. As an example, this is what Grensesnitt does.

like image 27
Mark Seemann Avatar answered Oct 31 '22 18:10

Mark Seemann