I am new to XUnit and Moq. I am attempting a simple test, but can't get it to work.
I have 7 test records and would like to add an 8th for my test.
My test always fails with the original 7 test records instead of 8.
I have setup my test per below..
public class CreateHandlerTests
{
private readonly Mock<IUnitOfWork> _mockUnitOfWork;
private readonly Mock<IGenericRepository<ServerAccess>> _mockRepo;
public CreateHandlerTests()
{
_mockUnitOfWork = new Mock<IUnitOfWork>();
_mockRepo = new Mock<IGenericRepository<ServerAccess>>();
}
[Fact]
public async Task ServerAccess_Created()
{
var list = MockData.GetServerAccessList(); // 7 test records
_mockRepo.Setup(x => x.CountAsync()).ReturnsAsync(list.Count);
_mockRepo.Setup(x => x.Add(It.IsAny<ServerAccess>())).Callback<ServerAccess>(item => list.Add(item));
_mockUnitOfWork.Setup(x => x.Repository<ServerAccess>()).Returns(_mockRepo.Object);
var before = await _mockRepo.Object.CountAsync();
var sut = new CreateHandler(_mockUnitOfWork.Object);
await sut.Execute(new CreateCommand()
{
AccessMethod = "New server access method",
Description = "New description"
});
var after = await _mockRepo.Object.CountAsync();
// Assert
Assert.True(after==8);
}
...
}
It appears that when my command handler adds a record, the Add method in my Mock IGenericRepository is not called.
Below is my IUnitOfWork interface
public interface IUnitOfWork
{
IGenericRepository<TEntity> Repository<TEntity>() where TEntity : BaseEntity;
Task<int> Complete();
}
Here my command handler implementation
public class CreateHandler : ICommandHandler<CreateCommand>
{
private readonly IUnitOfWork _unitOfWork;
public CreateHandler(IUnitOfWork unitOfWork)
{
_unitOfWork = Guard.Against.Null(unitOfWork, nameof(unitOfWork));
}
public async Task Execute(CreateCommand command)
{
var serverAccess = new ServerAccess()
{
AccessMethod = command.AccessMethod,
Description = command.Description
};
_unitOfWork.Repository<ServerAccess>().Add(serverAccess);
await _unitOfWork.Complete();
}
}
Did I miss something in my mock setup?
Any help is appreciated.