public async void Post_NoEstablishment_StatusCode_404_Test() { // 0: Remove all establishments from database await establishmentService.RemoveAll(); // 1: Creating testing objects ReleaseRequest testRelease = new ReleaseRequest() { Date = "05/05/2019", PaymentMethod = PaymentMethod.Credito, EstablishmentName = "Test 1", Amount = 55.55m }; // 2: Trying to POST a new release var query = await releasesController.Post(testRelease); var result = query.Result.GetType().GetProperty("Value").GetValue(query.Result); // 3: Check if result is an error Type resultType = result.GetType(); Assert.Equal(404, (int)resultType.GetProperty("StatusCode").GetValue(result)); Assert.Equal(controllerMessages.CantFoundGivenName.Replace("$", "estabelecimento"), (string)resultType.GetProperty("Message").GetValue(result)); }
public async void Put_SuccessStatus200_Test() { // 1: POST Request body var postRequestBody = new ReleaseRequest() { Date = "05/05/2019", PaymentMethod = PaymentMethod.Credito, EstablishmentName = "Test 1", Amount = 55.55m }; var establishmentServiceMock = new Mock <EstablishmentService>(dbSettings); establishmentServiceMock.Setup(es => es.GetByName(It.IsAny <string>())).ReturnsAsync( new Establishment() { Name = "Test 1", Type = "Alimentação" }); // 2: Call POST Action passing body request with a new release var releasesControllerLocal = new ReleasesController(loggerWrapper, releasesService, establishmentServiceMock.Object, controllerMessages); var postQuery = await releasesControllerLocal.Post(postRequestBody); var postResultValue = (Release)postQuery.Result.GetType().GetProperty("Value").GetValue(postQuery.Result); // 3: PUT Request body string id = postResultValue.Id; var requestBody = new ReleaseRequest { Date = "06/05/2019", PaymentMethod = PaymentMethod.Credito, EstablishmentName = "Test 1", Amount = 55.55m }; var query = await releasesControllerLocal.Put(id, requestBody); var statusCode = (int)query.Result.GetType().GetProperty("StatusCode").GetValue(query.Result); var result = (Release)query.Result.GetType().GetProperty("Value").GetValue(query.Result); Assert.Equal(200, statusCode); Assert.Equal("06/05/2019", result.Date); }
public async void Post_ThrowsException_Test() { // 0: Creating testing objects ReleaseRequest testRelease = new ReleaseRequest() { Date = "05/05/2019", PaymentMethod = PaymentMethod.Credito, EstablishmentName = "Test 1", Amount = 55.55m }; // 1: Mocking GetByName Method to throws var establishmentServiceMock = new Mock <EstablishmentService>(dbSettings); establishmentServiceMock.Setup(es => es.GetByName(It.IsAny <string>())).ThrowsAsync(new InvalidOperationException()); var releasesControllerLocal = new ReleasesController(loggerWrapper, releasesService, establishmentServiceMock.Object, controllerMessages); // 2: Call POST Action and Expects to throws await Assert.ThrowsAsync <InvalidOperationException>(async() => await releasesControllerLocal.Post(testRelease)); }