Пример #1
0
        public async Task Given_invalid_character_id_during_delete_should_throw_an_exception()
        {
            //arrange
            var invalidCharacterId = 0;
            var repository         = new CharacterRepository(DbContext, new UnitOfWork(DbContext));

            //act, assert
            await Assert.ThrowsAsync <NotFoundException>(() => repository.DeleteAsync(invalidCharacterId));
        }
Пример #2
0
        public async Task DeleteAsync_given_non_existing_characterId_returns_false()
        {
            using (var connection = await CreateConnectionAsync())
                using (var context = await CreateContextAsync(connection))
                {
                    var repository = new CharacterRepository(context);

                    var deleted = await repository.DeleteAsync(0);

                    Assert.False(deleted);
                }
        }
Пример #3
0
        public async Task DeleteAsync_given_id_exists_context_SaveChanges()
        {
            var entity = new Character();
            var mock   = new Mock <IFuturamaContext>();

            mock.Setup(s => s.Characters.FindAsync(42)).ReturnsAsync(entity);

            var repository = new CharacterRepository(mock.Object);

            await repository.DeleteAsync(42);

            mock.Verify(s => s.SaveChangesAsync(default));
Пример #4
0
        public async Task DeleteAsync_given_id_not_exists_return_false()
        {
            var mock = new Mock <IFuturamaContext>();

            mock.Setup(s => s.Characters.FindAsync(42)).ReturnsAsync(default(Character));

            var repository = new CharacterRepository(mock.Object);

            var deleted = await repository.DeleteAsync(42);

            Assert.False(deleted);
        }
Пример #5
0
        public async Task DeleteAsync_given_id_exists_character_is_removed_from_context()
        {
            var entity = new Character();
            var mock   = new Mock <IFuturamaContext>();

            mock.Setup(s => s.Characters.FindAsync(42)).ReturnsAsync(entity);

            var repository = new CharacterRepository(mock.Object);

            await repository.DeleteAsync(42);

            mock.Verify(s => s.Characters.Remove(entity));
        }
        public async Task Delete_given_non_existing_character_does_not_call_SaveChangesAsync()
        {
            var character = new Character();
            var mock      = new Mock <IFuturamaContext>();

            mock.Setup(m => m.Characters.FindAsync(42)).ReturnsAsync(default(Character));

            using (var repository = new CharacterRepository(mock.Object))
            {
                await repository.DeleteAsync(42);
            }

            mock.Verify(m => m.SaveChangesAsync(default(CancellationToken)), Times.Never);
        }
        public async Task Delete_given_existing_character_removes_it()
        {
            var character = new Character();
            var mock      = new Mock <IFuturamaContext>();

            mock.Setup(m => m.Characters.FindAsync(42)).ReturnsAsync(character);

            using (var repository = new CharacterRepository(mock.Object))
            {
                await repository.DeleteAsync(42);
            }

            mock.Verify(m => m.Characters.Remove(character));
        }
        public async Task Delete_given_42_deletes_character_and_SaveChanges()
        {
            var character = new Character();
            var mock      = new Mock <IFuturamaContext>();

            mock.Setup(m => m.Characters.FindAsync(42)).ReturnsAsync(character);
            mock.Setup(m => m.Characters.Remove(character));

            using (var repository = new CharacterRepository(mock.Object))
            {
                await repository.DeleteAsync(42);
            }

            mock.Verify(m => m.SaveChangesAsync(default(CancellationToken)));
        }
Пример #9
0
        public async Task After_deleted_character_should_not_give_deleted_character()
        {
            //arrange
            var newCharacter = new Character()
            {
                CharacterId = 100,
                Name        = "Yoda"
            };
            var repository = new CharacterRepository(DbContext, new UnitOfWork(DbContext));

            //act
            await repository.CreateAsync(newCharacter);

            await repository.DeleteAsync(newCharacter.CharacterId);

            //assert
            DbContext.Characters.Find(newCharacter.CharacterId).Should().BeNull();
        }
Пример #10
0
        public async Task DeleteAsync_given_existing_characterId_deletes_and_returns_true()
        {
            using (var connection = await CreateConnectionAsync())
                using (var context = await CreateContextAsync(connection))
                {
                    var entity = new Character {
                        Name = "Fry", Species = "Human", Planet = "Earth"
                    };
                    context.Characters.Add(entity);
                    await context.SaveChangesAsync();

                    var id = entity.Id;

                    var repository = new CharacterRepository(context);

                    var deleted = await repository.DeleteAsync(id);

                    Assert.True(deleted);
                }
        }
Пример #11
0
        public async Task DeleteAsync_sends_delete()
        {
            var handler = new Mock <HttpMessageHandler>();

            handler.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.NoContent
            });

            var client = new HttpClient(handler.Object)
            {
                BaseAddress = new Uri("https://futurama.com/"),
            };

            var repository = new CharacterRepository(client);

            var result = await repository.DeleteAsync(42);

            Assert.True(result);

            handler.Protected().Verify(
                "SendAsync",
                Times.Once(),
                ItExpr.Is <HttpRequestMessage>(req =>
                                               req.Method == HttpMethod.Delete &&
                                               req.RequestUri == new Uri("https://futurama.com/api/characters/42")
                                               ),
                ItExpr.IsAny <CancellationToken>()
                );
        }