Example #1
0
        public async Task GetTodoItemsAsync_WithElements_ShouldReturnElements()
        {
            // Arrange
            var expectedTodoItems = new[] {
                CreateFakeTodoItem(1),
                CreateFakeTodoItem(2),
                CreateFakeTodoItem(3)
            };
            var options = GetInMemoryOptions();
            var mapper  = GetMapper();

            ClearDataBase(options);

            using (var context = new ApplicationDbContext(options))
            {
                List <Task> tasks = new List <Task>();

                foreach (var expectedTodoItem in expectedTodoItems)
                {
                    tasks.Add(context.TodoItems.AddAsync(expectedTodoItem));
                }

                await Task.WhenAll(tasks);

                await context.SaveChangesAsync();
            }

            // Act
            using (var context = new ApplicationDbContext(options))
            {
                var service   = new TodoItemService(context, mapper);
                var todoItems = await service.GetTodoItemsAsync();

                // Assert
                Assert.NotNull(todoItems);
                Assert.True(todoItems.Count == 3);

                for (int index = 0; index < todoItems.Count; index++)
                {
                    Assert.Equal($"TodoItem {index + 1}", todoItems[index].Name);
                }
            }

            ClearDataBase(options);
        }
Example #2
0
        public async Task GetTodoItemsAsync_WithNoElements_ShouldReturnEmpty()
        {
            // Arrange
            var options = GetInMemoryOptions();
            var mapper  = GetMapper();

            ClearDataBase(options);

            // Act
            using (var context = new ApplicationDbContext(options))
            {
                var service   = new TodoItemService(context, mapper);
                var todoItems = await service.GetTodoItemsAsync();

                // Assert
                Assert.NotNull(todoItems);
                Assert.Empty(todoItems);
            }

            ClearDataBase(options);
        }