'XUnit Test Moq failing
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.
Solution 1:[1]
The problem is because you are returning a fixed value
_mockRepo.Setup(x => x.CountAsync()).ReturnsAsync(list.Count);
The above is the same as
_mockRepo.Setup(x => x.CountAsync()).ReturnsAsync(7);
it will return 7 every time since it was setup with a fixed value.
Use a function/delegate that will be invoked every time the expected member is called.
[Fact]
public async Task ServerAccess_Created() {
var list = MockData.GetServerAccessList(); // 7 test records
_mockRepo
.Setup(x => x.CountAsync())
.ReturnsAsync(() => list.Count); //delegate invoked each time
_mockRepo
.Setup(x => x.Add(It.IsAny<ServerAccess>()))
.Callback(ServerAccess item => list.Add(item)); //delegate invoked each time
_mockUnitOfWork
.Setup(x => x.Repository<ServerAccess>())
.Returns(_mockRepo.Object); //fixed value
int before = await _mockRepo.Object.CountAsync();
CreateHandler sut = new CreateHandler(_mockUnitOfWork.Object);
await sut.Execute(new CreateCommand() {
AccessMethod = "New server access method",
Description = "New description"
});
int after = await _mockRepo.Object.CountAsync();
// Assert
Assert.True(after == 8);
}
However, the above test should be simplified to assert the actual expected behavior based on the subject under test
[Fact]
public async Task ServerAccess_Created() {
//Arrange
_mockUnitOfWork
.Setup(x => x.Repository<ServerAccess>())
.Returns(_mockRepo.Object);
CreateHandler sut = new CreateHandler(_mockUnitOfWork.Object);
//Act
await sut.Execute(new CreateCommand() {
AccessMethod = "New server access method",
Description = "New description"
});
// Assert
_mockRepo.Verify(x => x.Add(It.IsAny<ServerAccess>());
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
