I am trying to test my delete method for my service, to do this I am trying to add an item to the repository first. However, I don't think my mock repository add method is being called because _mockRepository.Object.GetAll()
is always null. I have tried stepping in with the debugger and it just skips over it too. What am I doing wrong?
public class CommentServiceTest
{
private Mock<IRepository<Comment>> _mockRepository;
private ICommentService _service;
private ModelStateDictionary _modelState;
public CommentServiceTest()
{
_modelState = new ModelStateDictionary();
_mockRepository = new Mock<IRepository<Comment>>();
_service = new CommentService(new ModelStateWrapper(_modelState), _mockRepository.Object);
}
[Fact]
public void Delete()
{
var comment = new Comment("BodyText")
{
Audio = new Audio(),
Date = DateTime.Now
};
_mockRepository.Object.Add(comment);
//Nothing in repository collection here still
var commentToDelete = _mockRepository.Object.GetAll().First();
_service.Delete(commentToDelete);
Assert.DoesNotContain(commentToDelete, _mockRepository.Object.GetAll());
}
}
public class Repository<T, TC> : IRepository<T> where T : class where TC : DbContext
{
private bool _disposed;
protected TC Context { get; }
public Repository()
{
}
public Repository(TC context)
{
Context = context;
}
public virtual IQueryable<T> GetAll()
{
return Context.Set<T>();
}
public virtual void Add(T entity)
{
Context.Set<T>().Add(entity);
Save();
}
public virtual void Save()
{
Context.SaveChanges();
}
}
}
Aucun commentaire:
Enregistrer un commentaire