Я новичок в модульном тестировании и Moq. Я написал 4 теста, которые все выдают одно и то же исключение, и я думаю, что мне нужно добавить эти параметры CombatHelperContext, IMapper при имитации CharacterRepository, но как мне это сделать? .
public class CharacterTests
{
[Fact]
public async Task GetAll_ReturnsAViewResult_WithAListOfCharacters()
{
// Arrange
var mockRepo = new Mock<CharacterRepository>();
mockRepo.Setup(repo => repo.GetAll())
.Returns(GetTestCharacters());
var controller = new CharacterController(mockRepo.Object);
// Act
var result = await controller.Get();
// Assert
var viewResult = Assert.IsType<ViewResult>(result);
var model = Assert.IsAssignableFrom<List<CharacterDto>>(
viewResult.ViewData.Model);
Assert.Equal(4, model.Count());
}
[Fact]
public async Task Delete_Character()
{
// Arrange
var mockRepo = new Mock<CharacterRepository>();
mockRepo.Setup(repo => repo.Delete(1));
var controller = new CharacterController(mockRepo.Object);
var characters = GetTestCharacters();
// Act
var result = await controller.Get();
// Assert
Assert.Null(characters.Where(c => c.Id == 1));
}
[Fact]
public async Task Post_NewCharacter_ReturnsInViewResult()
{
// Arrange
CharacterDto newCharacterDto = new CharacterDto();
newCharacterDto.Id = 5;
var mockRepo = new Mock<CharacterRepository>();
mockRepo.Setup(repo => repo.Add(newCharacterDto));
var controller = new CharacterController(mockRepo.Object);
var characters = GetTestCharacters();
// Act
var result = await controller.Get();
// Assert
var viewResult = Assert.IsType<ViewResult>(result);
var model = Assert.IsAssignableFrom<List<CharacterDto>>(
viewResult.ViewData.Model);
Assert.Equal(5, model.Count());
}
[Fact]
public async Task Update_Character()
{
// Arrange
var mockRepo = new Mock<CharacterRepository>();
var characters = GetTestCharacters();
CharacterDto characterToUpdate = characters.FirstOrDefault(c => c.Name == "Zero Two");
var controller = new CharacterController(mockRepo.Object);
// Act
var result = await controller.Get();
characterToUpdate.Name = "Zero Three";
mockRepo.Setup(repo => repo.Update(characterToUpdate));
// Assert
Assert.NotNull(characters.FirstOrDefault(c => c.Name == "Zero Three"));
}
private IQueryable<CharacterDto> GetTestCharacters()
{
var characters = new List<CharacterDto>();
characters.Add(new CharacterDto()
{
Id = 1,
Name = "Zero Two"
});
characters.Add(
new CharacterDto
{
Id = 2,
Name = "Ander"
});
characters.Add(
new CharacterDto
{
Id = 3,
Name = "Jingles"
});
characters.Add(new CharacterDto
{
Id = 4,
Name = "Arthas Menethil"
});
var queryableCharacters = characters.AsQueryable();
return queryableCharacters;
}
}
Репозиторий, над которым я издеваюсь
public class CharacterRepository : MappingRepository<Character, CharacterDto>
{
public CharacterRepository(CombatHelperContext combatHelperContext, IMapper mapper) : base(combatHelperContext, mapper)
{
}
}