public async Task CreateAsync_Should_CreateTodo() { //Setup DbContext and DbSet mock var dbContextMock = new Mock <ITodoContext>(); var testTodo = new Todo { TodoId = 3, Title = "test todo 3", Points = new List <TodoPoint> { } }; var testTodos = GetTestTodos(); var todoDbSetMock = testTodos.AsQueryable().GetMockDbSet(); dbContextMock.Setup(x => x.DbSet <Todo>()).Returns(todoDbSetMock.Object); todoDbSetMock.Setup(x => x.Add(It.IsAny <Todo>())) .Callback(() => { testTodos.Add(testTodo); }); //Execute var todoService = new TodoService(dbContextMock.Object); await todoService.CreateAsync(testTodo); var todo = await todoService.GetAsync <Todo>(x => x.TodoId == testTodo.TodoId); //Assert Assert.IsAssignableFrom <Todo>(todo); Assert.Equal(testTodo.Title, todo.Title); Assert.Equal(3, testTodos.Count); }
public async Task <IActionResult> CreateNoteAsync([FromBody] ClientModels.Todos.TodoBuildInfo buildInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (buildInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("buildInfo"); return(this.BadRequest(error)); } var nameIdentifier = this.HttpContext.User.Claims .FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier); var user = await _userRepository.GetAsync(nameIdentifier.Value, cancellationToken).ConfigureAwait(false); var creationInfo = TodoBuildInfoConverter.Convert(user.Id.ToString(), buildInfo); var modelTodoInfo = await _todoService.CreateAsync(creationInfo, cancellationToken).ConfigureAwait(false); var clientTodoInfo = TodoInfoConverter.Convert(modelTodoInfo); var routeParams = new Dictionary <string, object> { { "recordId", clientTodoInfo.Id } }; return(CreatedAtRoute("GetTodoRoute", routeParams, clientTodoInfo)); }
protected override void Context() { // Arrange _repositoryMock = new Mock <ITodoRepository>().SetupCreateAsyncToReturnItem(); _sut = new TodoService(_repositoryMock.Object); _returnedTodo = _sut.CreateAsync(new TodoItem { Id = 1, Name = "name", IsComplete = false }).Result; }