public async Task DeleteNonExistingSupplier() { // arrange HAVIdatabaseContext dbContext = CreateDbContext(); SupplierRepository repository = new SupplierRepository(dbContext); Supplier supplier = new Supplier() { Id = 1, ProfileId = 0, CompanyName = "Name", CompanyLocation = "Location", FreightResponsibility = "EXW", PalletExchange = 1, Profile = new Profile() { Id = 0, Username = "******", Password = "******", Usertype = 2 } }; // act Supplier result = await repository.DeleteSupplierAsync(supplier.Id); // assert Assert.IsTrue(result == null); dbContext.Dispose(); }
public async Task DeleteSupplierAsync_Returns_NoResult() { //Arrange var id = 2; _fixture.MockSupplierService.Setup(x => x.GetSupplierAsync(It.IsAny <Expression <Func <Supplier, bool> > >())) .Returns <Expression <Func <Supplier, bool> > >(expression => Task.FromResult(_fixture.Suppliers.AsQueryable().FirstOrDefault(expression))); _fixture.MockSupplierService.Setup(x => x.DeleteSupplierAsync(It.IsAny <Supplier>())); var repository = new SupplierRepository(AutoMapperSingleton.Mapper, _fixture.MockSupplierService.Object); //Act await repository.DeleteSupplierAsync(id); // Assert _fixture.MockSupplierService.Verify(x => x.DeleteSupplierAsync(It.IsAny <Supplier>()), Times.Once); }
public async Task <ActionResult <Supplier> > DeleteSupplier(int id) { try { var supplierToDelete = await _supplierRepository.GetSupplier(id); if (supplierToDelete == null) { return(NotFound($"Supplier with id = {id} not found")); } return(await _supplierRepository.DeleteSupplierAsync(id)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database.")); } }
public async Task DeleteSupplierAsync_Throws_NotFoundException() { //Arrange var id = 201; _fixture.MockSupplierService.Setup(x => x.GetSupplierAsync(It.IsAny <Expression <Func <Supplier, bool> > >())) .Returns <Expression <Func <Supplier, bool> > >(expression => Task.FromResult(_fixture.Suppliers.AsQueryable().FirstOrDefault(expression))); _fixture.MockSupplierService.Setup(x => x.DeleteSupplierAsync(It.IsAny <Supplier>())); var repository = new SupplierRepository(AutoMapperSingleton.Mapper, _fixture.MockSupplierService.Object); //Act var exception = await Assert.ThrowsAsync <RestException>(() => repository.DeleteSupplierAsync(id)); //Assert exception.ErrorCode.Should().Be(HttpStatusCode.NotFound); exception.ErrorMessage.Should().Be("Supplier not found."); exception.ErrorType.Should().Be(HttpStatusCode.NotFound.ToString()); }