Molto probabilmente questi oggetti sono creati da una fabbrica o da un singolo punto in cui il grafico dell'oggetto viene creato come indicato dai commenti di @ guillaume31. Se dalla fabbrica assomiglierebbe a questo:
namespace Repository.Lookup
{
public class LookupRepositoryFactory
{
public static ILookupRepository Create()
{
ILookupRepository lookupRepository = new LookupRepository();
return lookupRepository;
}
}
}
Quindi il test per la fabbrica:
namespace RepositoryTests.Lookup
{
[TestClass]
public class LookupRepositoryFactoryTests
{
[TestMethod]
public void TestCreateLookupRepository()
{
var expectedType = typeof(LookupRepository);
var repository = LookupRepositoryFactory.Create();
Assert.IsInstanceOfType(repository, expectedType);
}
}
}
Quindi uso:
var lookupRepository = LookupRepositoryFactory.Create();
Poi lo hai appena passato al costruttore come hai detto, in questo modo:
namespace CommonLookupAPI
{
public class GenericLookup
{
private readonly ILookupRepository _repository;
public GenericLookup(ILookupRepository repository)
{
_repository = repository;
}
public List<CommonLookup> GetCommonLookup(LineOfBusiness lineOfBusiness, ClassificationType classificationType,
State state, Grouping grouping, SubGrouping subGrouping)
{
var commonLookups = _repository.GetCommonLookup(lineOfBusiness, classificationType, state, grouping, subGrouping);
return commonLookups;
}
}
}
Ciò consente di prendere in giro il repository con una falsa implementazione in modo da poter testare la ricerca generica senza utilizzare il repository reale.
Ecco i test:
namespace CommonLookupAPITests
{
[TestClass]
public class GenericLookupTests
{
[TestMethod]
public void TestGenericLookupReturnsLookup()
{
ILookupRepository repository = MockRepositories.CreateMockSuccessRepository();
var genericLookup = new GenericLookup(repository);
var lookups = genericLookup.GetCommonLookup(LineOfBusiness.AssistedLivingProfessionalLiability, ClassificationType.ClaimLimit, State.NotDefined, Grouping.NotDefined, SubGrouping.NotDefined);
Assert.AreEqual(lookups.Count, 0);
}
[TestMethod]
[ExpectedException(typeof(Exception))]
public void TestGenericLookupReturnsFailUnExpected()
{
ILookupRepository repository = MockRepositories.CreateMockFailRepositoryException();
var genericLookup = new GenericLookup(repository);
var lookups = genericLookup.GetCommonLookup(LineOfBusiness.AssistedLivingProfessionalLiability, ClassificationType.ClaimLimit, State.NotDefined, Grouping.NotDefined, SubGrouping.NotDefined);
}
[TestMethod]
[ExpectedException(typeof(DataAccessException))]
public void TestGenericLookupReturnsFailDataAccess()
{
ILookupRepository repository = MockRepositories.CreateMockFailRepositoryDataAccessException();
var genericLookup = new GenericLookup(repository);
var lookups = genericLookup.GetCommonLookup(LineOfBusiness.AssistedLivingProfessionalLiability, ClassificationType.ClaimLimit, State.NotDefined, Grouping.NotDefined, SubGrouping.NotDefined);
}
}
}
E infine i Repository di simulazione utilizzati dai test:
namespace CommonLookupAPITests
{
public class MockRepositories
{
public static ILookupRepository CreateMockSuccessRepository()
{
ILookupRepository repository = new MockSuccessRepository();
return repository;
}
public static ILookupRepository CreateMockFailRepositoryException()
{
ILookupRepository repository = new MockFailRepositoryException();
return repository;
}
public static ILookupRepository CreateMockFailRepositoryDataAccessException()
{
ILookupRepository repository = new MockFailRepositoryDataAccessException();
return repository;
}
}
public class MockSuccessRepository : ILookupRepository
{
public List<CommonLookup> GetCommonLookup(LineOfBusiness lineOfBusiness, ClassificationType classificationType,
State state, Grouping grouping, SubGrouping subGrouping)
{
var commonLookup = new List<CommonLookup>();
return commonLookup;
}
}
public class MockFailRepositoryException : ILookupRepository
{
public List<CommonLookup> GetCommonLookup(LineOfBusiness lineOfBusiness, ClassificationType classificationType,
State state, Grouping grouping, SubGrouping subGrouping)
{
throw new Exception();
}
}
public class MockFailRepositoryDataAccessException : ILookupRepository
{
public List<CommonLookup> GetCommonLookup(LineOfBusiness lineOfBusiness, ClassificationType classificationType,
State state, Grouping grouping, SubGrouping subGrouping)
{
throw new DataAccessException();
}
}
}