-2

Is there a collection of unit tests that will verify my implementation of an interface is correct? Specifically, I am using an implementation of ISet<T> and would like to basically plug it into a unit test to verify my implementation is correct. (Rather than me coming up with 5-20 unit tests for good coverage)

It would seem like all interfaces could be have some general unit tests written for them.

ParoX
  • 5,685
  • 23
  • 81
  • 152
  • not sure what you mean, you're supposed to have unit tests that reflect the scope of an implementation.. how would unit testing an interface (which has no implementation or method scope) work exactly? also, this sort question is likely not an SO question, more like a software engineering one. – Brett Caswell Sep 03 '18 at 04:17
  • There is some logic/expectation of some interface methods, though, for example `Add(T item)` should add an item, there could be a unit test to verify it adds an item correctly, etc. – ParoX Sep 03 '18 at 04:25
  • hmm, that doesn't sound like a good unit test.. a good unit test would be `ISomeObjectMock.Add(T item)` was called 1 or more times, when this condition was met in this Service Method. – Brett Caswell Sep 03 '18 at 04:28
  • regarding generics and types.. my company uses `Fizzware` and for type argument notions like the concept I mentioned above, you would `It.IsAny` (where `T` is defined in your test and expectations) when building the mock service. see this SO answer for a full sample: https://stackoverflow.com/a/36345351/1366179 – Brett Caswell Sep 03 '18 at 04:40

2 Answers2

0

There is helpful post regarding the matter in Software Engineering the code will look something like this:

private void Test<TService,TFilter>(string urlSuffix) 
    where TService : SoapHttpClientProtocol, new() 
    where TFilter : new() 
{
    string serviceUrl = Helper.GetBaseUrl() + urlSuffix;
    TService service = new TService();
    service.Credentials = Helper.GetCredentials();
    service.Url = serviceUrl;
    TFilter[] filter = { new TFilter() };
    service.ReadMultiple(filter, null, 0);
}

[TestMethod]
public void PlaceOfWork()
{
    Test<PlaceOfWork_Service,PlaceOfWork_Filter>("PlaceOfWork");
}

This is a generic method to mock service connection and access but it can easily be applied to interfaces as well.

However Be ware that if you use generics in unit test you lose granularity, this is because your test is going to cover multiple cases.

  1. It will soon become unclear what you are testing in each interface

  2. If your test fails after the first one, you fail to uncover more bugs resulting in fail masking..

Check this solution, twitch with it a little bit in order to understand all of it's advantages and disadvantages and read similar posts as well.

Barr J
  • 10,636
  • 1
  • 28
  • 46