public async Task AddNewItem() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "Test_AddNewItem").Options; using (var inMemoryContext = new ApplicationDbContext(options)) { var service = new TodoItemsService(inMemoryContext); var fakeUser = new ApplicationUser { Id = "fake-000", UserName = "******" }; await service.AddItemAsync(new NewTodoItem { Title = "Testing" }, fakeUser); } using (var inMemoryContext = new ApplicationDbContext(options)) { Assert.Equal(1, await inMemoryContext.Items.CountAsync()); var item = await inMemoryContext.Items.FirstAsync(); Assert.Equal("Testing", item.Title); Assert.False(item.IsDone); Assert.True(DateTimeOffset.Now.AddDays(3) - item.DueAt < TimeSpan.FromSeconds(1)); } }
public async Task GetTodoItem_When_TodoItemExists_Then_CallMapperWithItemReturnedFromRepository() { // Arrange var mapperMock = Substitute.For <ITodoItemsMapper>(); var repositoryMock = Substitute.For <ITodoItemsRepository>(); var todoItemId = 1; var todoItem = new TodoItem() { Id = todoItemId, IsComplete = true, Name = "TestName" }; repositoryMock.GetTodoItem(todoItemId).Returns(todoItem); var cut = new TodoItemsService(repositoryMock, mapperMock); // Act await cut.GetTodoItem(todoItemId); // Assert #pragma warning disable CS4014 repositoryMock.Received().GetTodoItem(Arg.Is <long>(todoItemId)); #pragma warning restore CS4014 mapperMock.Received().ItemToDTO(Arg.Is(todoItem)); }
public static void Main(string[] args) { bool exit = false; while (!exit) { //Print Menu Console.WriteLine(); Console.WriteLine(" ---MENU---"); Console.WriteLine(" 1. List my items"); Console.WriteLine(" 2. Add new item"); Console.WriteLine(" 3. Exit"); Console.WriteLine(); string option = Console.ReadLine(); int parsedOption; //Validate the option: not empty, and a valid integer if (!string.IsNullOrEmpty(option) && int.TryParse(option, out parsedOption)) { var context = new TodoAppContext(); TodoItemsService service = new TodoItemsService(context); switch (parsedOption) { case 1: //Accesing the data Console.WriteLine(" We have this items:"); var items = service.GetItems(); foreach (var item in items) { Console.WriteLine(" {Id:" + item.Id + ", Date:" + item.DateTime.ToShortTimeString() + ", Name:" + item.Name + "}"); } break; case 2: Console.WriteLine(" Please provide a name for the item:"); string newItemName = Console.ReadLine(); service.Add(new TodoItem() { Name = newItemName }); Console.WriteLine(" Item added successfully!"); break; case 3: exit = true; break; default: break; } } else { exit = true; } } }
private TodoItemsService GetService(string fakeDatabaseName) { var options = new DbContextOptionsBuilder <TodoAppContext>() .UseInMemoryDatabase(databaseName: fakeDatabaseName) .Options; //EF Core uses a simplier approach to an in-memmory database var context = new TodoAppContext(options); TodoItemsService itemsService = new TodoItemsService(context); return(itemsService); }
public async Task GetTodoItem_When_TodoItemDoesNotExist_Then_ReturnNull() { // Arrange var mapperMock = Substitute.For <ITodoItemsMapper>(); var repositoryMock = Substitute.For <ITodoItemsRepository>(); var todoItemId = 1; repositoryMock.GetTodoItem(todoItemId).Returns(default(TodoItem)); var cut = new TodoItemsService(repositoryMock, mapperMock); // Act var result = await cut.GetTodoItem(todoItemId); // Assert #pragma warning disable CS4014 repositoryMock.Received().GetTodoItem(Arg.Any <long>()); #pragma warning restore CS4014 mapperMock.DidNotReceive().ItemToDTO(Arg.Any <TodoItem>()); result.Should().BeNull(); }
public TodoItemsController(TodoItemsService todoItemService) { _todoItemService = todoItemService; }