public async Task CreateBeer_GivenAValidBeer_ShouldCallCreateBeerWithACorrectlyMappedBeerEntity() { //Arrange var beer = new Beer { Name = "Test name" }; var beerEntity = beer.ToEntity(); var mockedValidation = new Mock <IBrandValidation>(); mockedValidation.Setup(validation => validation.ValidateBeer(It.IsAny <Beer>())); var mockedDataAccess = new Mock <IBrandDataAccess>(); mockedDataAccess.Setup(dataAccess => dataAccess.CreateBeer(It.IsAny <BeerEntity>())) .ReturnsAsync(Guid.Empty); var brandService = new BrandService(mockedDataAccess.Object, mockedValidation.Object); //Act var result = await brandService.CreateBeer(beer); //Assert mockedDataAccess.Verify(dataAccess => dataAccess.CreateBeer(It.Is <BeerEntity>(entity => Serialize(entity) == Serialize(beerEntity))), Times.Once); }
public async Task CreateBeer_GivenAnInvalidBeer_ShouldThrowABadRequestHttpResult() { //Arrange var beer = new Beer(); var mockedValidation = new Mock <IBrandValidation>(); mockedValidation.Setup(validation => validation.ValidateBeer(It.IsAny <Beer>())) .ThrowsAsync(new HttpException(HttpStatusCode.BadRequest, "Bad request")); var brandService = new BrandService(null, mockedValidation.Object); //Assert await Assert.ThrowsAsync <HttpException>(async() => await brandService.CreateBeer(beer)); }
public async Task CreateBeer_GivenAValidBeer_ShouldReturnTheGuidCreatedByBrandPersistance() { //Arrange var guid = Guid.NewGuid(); var beer = new Beer(); var mockedBrandPersistance = new Mock <IBrandPersistence>(); mockedBrandPersistance.Setup(persistance => persistance.CreateBeer(It.IsAny <Beer>())) .ReturnsAsync(guid); var brandService = new BrandService(null, mockedBrandPersistance.Object); //Act var result = await brandService.CreateBeer(beer); //Assert Assert.Equal(guid.ToString(), result.ToString()); mockedBrandPersistance.Verify(persistance => persistance.CreateBeer(It.IsAny <Beer>()), Times.Once); }
public async Task CreateBeer_GivenAValidBeer_ShouldCallValidationAndCreateBeer() { //Arrange var beer = new Beer(); var mockedValidation = new Mock <IBrandValidation>(); mockedValidation.Setup(validation => validation.ValidateBeer(It.IsAny <Beer>())); var mockedDataAccess = new Mock <IBrandDataAccess>(); mockedDataAccess.Setup(dataAccess => dataAccess.CreateBeer(It.IsAny <BeerEntity>())) .ReturnsAsync(Guid.Empty); var brandService = new BrandService(mockedDataAccess.Object, mockedValidation.Object); //Act var result = await brandService.CreateBeer(beer); //Assert mockedValidation.Verify(validation => validation.ValidateBeer(It.IsAny <Beer>()), Times.Once); mockedDataAccess.Verify(dataAccess => dataAccess.CreateBeer(It.IsAny <BeerEntity>()), Times.Once); }