public void Post_WhenInvoked_ShouldCallAddAsync() { //arrange this.mockUserResolverService .Setup(x => x.User) .Returns(new ClaimsPrincipal(new GenericIdentity(Guid.NewGuid().ToString()))); var bindindModel = new AddExpenseBindingModel { Amount = 100, CategoryId = 1, Description = "test1" }; //act var response = this.testClient .PostAsJsonAsync(TestContants.EXPENSES_ENDPOINT, bindindModel).Result; //assert response.EnsureSuccessStatusCode(); Assert.AreEqual(response.StatusCode, HttpStatusCode.Created); this.mockExpenseService.Verify(x => x.AddAsync(It.IsAny <decimal>(), It.IsAny <string>(), It.IsAny <long>(), It.IsAny <string>()), Times.Once); }
public async Task <ActionResult <Expense> > PostExpense(AddExpenseBindingModel model) { var expense = new Expense { Description = model.Description, Amount = model.Amount, Category = model.Category, SpentDate = model.SpentDate, CreationDate = DateTime.UtcNow, }; await this.expenseRepository.AddAsync(expense); return(this.CreatedAtAction("GetExpense", new { id = expense.Id }, expense)); }
public async Task <IActionResult> AddAsync(AddExpenseBindingModel bindingModel) { try { var userId = this.userResolverService.User.FindFirst(ClaimTypes.Name).Value; var expense = await this.expensesService.AddAsync( bindingModel.Amount, bindingModel.Description, bindingModel.CategoryId, userId); return(this.Created("", expense)); } catch (Exception e) { return(this.BadRequest("Unable to add expense, please contact support")); } }
public async Task <Expense> AddAsync( string description, decimal amount, Category category, DateTime spentDate) { var expense = new AddExpenseBindingModel { Description = description, Amount = amount, Category = category, SpentDate = spentDate }; var expenseJson = new StringContent(JsonSerializer.Serialize(expense), Encoding.UTF8, "application/json"); var response = await this.httpClient.PostAsync(URL_BASE, expenseJson); if (response.IsSuccessStatusCode) { return(await JsonSerializer.DeserializeAsync <Expense>(await response.Content.ReadAsStreamAsync())); } return(null); }