vendredi 27 février 2015

Pointless unit test

I have a baseService class that most of my services inherit from, which looks like this.



public abstract class BaseService<T> : IBaseService<T>
where T : class, IBaseEntity
{
protected IDataContext _context;
protected IValidator<T> _validator = null;

protected BaseService(IDataContext context)
{
_context = context;
}

protected BaseService(IDataContext context, IValidator<T> validator)
: this(context)
{
_validator = validator;
}

public virtual async Task<ICollection<T>> GetAllAsync()
{
return await _context.Set<T>().ToListAsync();
}

public virtual Task<T> GetAsync(long id)
{
return _context.Set<T>().Where(e => e.Id == id).FirstOrDefaultAsync();
}

public virtual Task<ValidationResult> ValidateAsync(T t)
{
if (_validator == null) throw new MissingFieldException("Validator does not exist for class " + t.GetType().ToString() + ". override method if no validation needed");
return _validator.ValidateAsync(t);
}

public virtual async Task<int> AddAsync(T t)
{
var results = await ValidateAsync(t);

if (!results.IsValid) {
throw new ValidationException(results.Errors);
}

if (_context.GetState(t) == EntityState.Detached)
{
_context.Set<T>().Add(t);
_context.SetState(t, EntityState.Added);
}

return await _context.SaveChangesAsync();
}

public virtual async Task<int> UpdateAsync(T updated)
{
var results = await ValidateAsync(updated);

if (!results.IsValid)
{
throw new ValidationException(results.Errors);
}

if (_context.GetState(updated) == EntityState.Detached)
{
_context.SetState(updated, EntityState.Modified);
}

return await _context.SaveChangesAsync();
}

public virtual Task<int> DeleteAsync(T t)
{
_context.SetState(t, EntityState.Deleted);

return _context.SaveChangesAsync();
}
}


Am i right in thinking that it is pointless to unit test this in every single one of my classes that implements this service? But instead, test the functionality for each test in my integration testing?


Aucun commentaire:

Enregistrer un commentaire