public void Handle_IdIsEmpty_ShouldThrowValidationException() { var command = new ActivateReceiptCommand { ReceiptId = "" }; FluentActions.Invoking(async() => await SendAsync(command)).Should().Throw <ValidationException>(); }
public void Handle_InvalidId_ShouldThrowNotFoundException() { var command = new ActivateReceiptCommand { ReceiptId = "I DONT KNOW" }; FluentActions.Invoking(async() => await SendAsync(command)).Should().Throw <NotFoundException>(); }
public async Task <IActionResult> ActivateReceipt(string id) { var command = new ActivateReceiptCommand { ReceiptId = id }; await Mediator.Send(command); return(RedirectToAction("Index")); }
public async Task Handle_ValidIdButWrongUser_ShouldThrowNotFoundException() { var receiptId = await CreateReceipt(); var delete = new DeleteReceiptCommand { Id = receiptId }; await SendAsync(delete); await RunAsUserAsync("*****@*****.**", "nah"); var activate = new ActivateReceiptCommand { ReceiptId = receiptId }; FluentActions.Invoking(async() => await SendAsync(activate)).Should().Throw <NotFoundException>(); }
public async Task Handle_ValidId_ShouldActivateReceipt(int amount) { var receiptId = await CreateReceipt(); var item = GetReceiptItem(new Random().Next(10, 100000)); var command = new CreateReceiptItemCommand { Name = "dd", ItemGroup = item.ItemGroup.Value, Price = item.Price, Count = item.Count, ReceiptId = receiptId }; command.UserIds = amount switch { 1 => new List <string> { SecondUser.Id }, 2 => new List <string> { SecondUser.Id, User.Id }, _ => command.UserIds }; await SendAsync(command); var context = CreateContext(); //create var firstOweRecord = context.OweRecords.FirstOrDefault(x => x.UserId == SecondUser.Id && x.OwedUserId == User.Id); firstOweRecord.Should().NotBeNull(); firstOweRecord !.Amount.Should().Be(command.Count * command.Price / command.UserIds.Count); //10k var deleteCommand = new DeleteReceiptCommand { Id = receiptId }; await SendAsync(deleteCommand); context = CreateContext(); //delete var deletedEntity = context.Receipts.FirstOrDefault(x => x.Id == receiptId); deletedEntity.Should().NotBeNull(); deletedEntity !.Deleted.Should().BeCloseTo(DateTime.Now, 1000); deletedEntity !.DeletedByUserId.Should().Be(User.Id); var secondOweRecord = context.OweRecords.FirstOrDefault(x => x.UserId == SecondUser.Id && x.OwedUserId == User.Id); secondOweRecord.Should().NotBeNull(); secondOweRecord !.Amount.Should().Be(0); //0 //activate var reactivateCommand = new ActivateReceiptCommand { ReceiptId = receiptId }; await SendAsync(reactivateCommand); context = CreateContext(); var thirdOweRecord = context.OweRecords.FirstOrDefault(x => x.UserId == SecondUser.Id && x.OwedUserId == User.Id); thirdOweRecord.Should().NotBeNull(); thirdOweRecord !.Amount.Should().Be(command.Count * command.Price / command.UserIds.Count); var activatedEntity = context.Receipts.FirstOrDefault(x => x.Id == receiptId); activatedEntity.Should().NotBeNull(); activatedEntity !.Deleted.Should().BeNull(); activatedEntity !.DeletedByUserId.Should().BeNull(); }