My code basically looks like this:
Data access contract:
public interface IProvideDataAccess<T>
where T : Entity
{
IEnumerable<T> Select(Func<T, bool> condition);
void Save(T entity);
void Delete(T entity);
}
Data access layer:
public class Db4oProvider<T> : IProvideDataAccess<T>
where T : Entity
{
private IEmbeddedConfiguration _configuration;
private string _connectionString;
public Db4oAccesDonnees(string connectionString)
{
_connectionString = connectionString;
_configuration = Db4oEmbedded.NewConfiguration();
}
IEnumerable<T> IProvideDataAccess<T>.Select(Func<T, bool> condition)
{
using (IObjectContainer db = Db4oEmbedded.OpenFile(_configuration, _connexion))
{
return db.Query<T>(e => condition(e));
}
}
void IProvideDataAccess<T>.Save(T entity)
{
using (IObjectContainer db = Db4oEmbedded.OpenFile(_configuration, _connexion))
{
db.Store(entity);
}
}
void IProvideDataAccess<T>.Delete(T entity)
{
using (IObjectContainer db = Db4oEmbedded.OpenFile(_configuration, _connexion))
{
db.Delete(entity);
}
}
}
Business logic layer:
public class MyRepository
{
protected IProvideDataAccess<MyEntityType> _dataAccessProvider;
public MyRepository(IProvideDataAccess<MyEntityType> dataAccessProvider)
{
_dataAccessProvider = dataAccessProvider;
}
public IEnumerable<MyEntityType> SelectValidEntities()
{
return _dataAccessProvider.Select(e => /* test if entity is valid */);
}
}
Unit/Integration tests are fairly new to me and I'm not sure where to begin.
I think the logical thing to do would be to
- write integration tests for the DAL
- write unit tests for the BLL (with fake DAL)
I this correct?
My biggest problem is with the "select" method and its Func parameter. How do I test/mock that?
Basically, what tests should I write for these two classes?