Exemplo n.º 1
0
        public GetCharacterTests()
        {
            _character = new Character {
                Id = 1, Name = "Alex"
            };

            var characterRepositoryMock = new Mock <ICharacterSystemCharacterRepository>();

            characterRepositoryMock
            .Setup(a => a.GetCharacter(It.IsAny <int>()))
            .ReturnsAsync((int id) => id == _character.Id ? _character : null);
            _characterRepository = characterRepositoryMock.Object;

            _handler = new GetCharacterQueryHandler(_characterRepository);
        }
        public async Task Get_character_query_handler_should_throw_not_found_exception()
        {
            // Arrange
            var character = new CharacterBuilder().Generate().Build().First();
            var query     = new GetCharacterQuery {
                Id = character.Id + 1
            };
            var queryHandler = new GetCharacterQueryHandler(_testFixture.Context, _testFixture.Mapper);

            // Act
            async Task Act() => await queryHandler.Handle(query, CancellationToken.None);

            var ex = await Record.ExceptionAsync(Act);

            // Assert
            ex.ShouldBeOfType <NotFoundException>();
        }
        public async Task Get_character_query_handler_should_pass()
        {
            // Arrange
            var character = new CharacterBuilder().Generate().Build().First();
            var episode   = new EpisodeBuilder().Generate().SaveChanges(_testFixture.Context).Build().First();

            character.Episodes.Add(new CharacterEpisode {
                CharacterId = character.Id, EpisodeId = episode.Id
            });
            _testFixture.Context.Characters.Update(character);
            _testFixture.Context.SaveChanges();
            var query = new GetCharacterQuery {
                Id = character.Id
            };
            var queryHandler = new GetCharacterQueryHandler(_testFixture.Context, _testFixture.Mapper);

            // Act
            var response = await queryHandler.Handle(query, CancellationToken.None);

            // Assert
            response.Name.ShouldBe(character.Name);
            response.Episodes.Count.ShouldBe(1);
            response.Episodes.First().Name.ShouldBe(episode.Name);
        }