• 1000 Проблема не возникает при чтении из DbContext.
Тестовый код:
public class DbBoardGameServiceTest : IDisposable
{
private readonly TestDbFactory _testDbFactory;
private readonly BoardGamesDbContext _dbContext;
private readonly Mock<IMapper> _mapperMock = new Mock<IMapper>();
private readonly DbBoardGameService _sut;
public DbBoardGameServiceTest()
{
_testDbFactory = new TestDbFactory();
_dbContext = _testDbFactory.CreateDbContext();
_sut = new DbBoardGameService(_dbContext, _mapperMock.Object);
}
[Fact(DisplayName = "All BoardGames from a database are returned after being mapped when service gets all board games")]
public void All_BoardGames_from_a_database_are_returned_when_after_being_mapped_service_gets_all_board_games()
{
_mapperMock.Setup(mock => mock.Map<BoardGameDto>(It.IsAny<Models.BoardGame>()))
.Returns<Models.BoardGame>(boardGame => DtoWithName(boardGame.Name));
_dbContext.BoardGames.AddRange(TestBoardGames); //this line causes stackoverflow
var games = _dbContext.BoardGames.ToArray(); // this line does not cause any errors
var actualBoardGames = _sut.GetAllBoardGames().ToArrayAsync().Result;
var expectedBoardGames = new[]
{
DtoWithName("Name1"),
DtoWithName("Name2"),
DtoWithName("Name3"),
DtoWithName("Name4"),
};
actualBoardGames.Should().BeEquivalentTo(expectedBoardGames);
}
private static Models.BoardGame[] TestBoardGames => new[]
{
new BoardGameBuilder().WithName("Name1").WithId(1).Build(),
new BoardGameBuilder().WithName("Name2").WithId(2).Build(),
new BoardGameBuilder().WithName("Name3").WithId(3).Build(),
new BoardGameBuilder().WithName("Name4").WithId(4).Build()
};
private static BoardGameDto DtoWithName(string name) => new BoardGameDto(0, name, 0, 0, 0);
public void Dispose() => _testDbFactory.Dispose();
}
Я полностью потерялся, я обнаружил информацию, что переполнение стека во время тестов может быть вызвано круговыми зависимостями, но я сомневаюсь, что это так. Приветствуются любые подсказки.
РЕДАКТИРОВАТЬ: он не работает на Ubuntu 20.04, но на macOS он работает нормально.