public async void Delete_Returns406_NotAcknowledged_Test() { // 0: Remove all establishments from database await establishmentService.RemoveAll(); // 1: Request body string id = "5dcaad2526235a471cfcccad"; // 2: Mocking GetById Method to return fake data var fakeEstablishment = new Establishment { Id = id, Name = "Test 1", Type = "Tipo 1" }; var establishmentServiceMock = new Mock <EstablishmentService>(dbSettings); establishmentServiceMock.Setup(es => es.GetById(It.IsAny <string>())).ReturnsAsync(fakeEstablishment); var deleteResultWrapper = new DeleteResultWrapper(); establishmentServiceMock.Setup(es => es.RemoveById(It.IsAny <string>())).ReturnsAsync(deleteResultWrapper); var establishmentsControllerLocal = new EstablishmentsController(loggerWrapper, establishmentServiceMock.Object, controllerMessages); var query = await establishmentsControllerLocal.Delete(id); var result = (ResponseDetails)query.Result.GetType().GetProperty("Value").GetValue(query.Result); Assert.Equal(406, result.StatusCode); Assert.Equal(controllerMessages.CantRemove, result.Message); }
public async void Get_ThrowsException_Test() { // 1: Mocking GetAll Method to throws var establishmentServiceMock = new Mock <EstablishmentService>(dbSettings); establishmentServiceMock.Setup(es => es.GetAll()).ThrowsAsync(new InvalidOperationException()); var establishmentsControllerLocal = new EstablishmentsController(loggerWrapper, establishmentServiceMock.Object, controllerMessages); // 2: Call GET Action and Expects to throws await Assert.ThrowsAsync <InvalidOperationException>(async() => await establishmentsControllerLocal.Get()); }
public async void Delete_ThrowsException_Test() { // 1: Request id string id = "5dcaad2526235a471cfcccad"; // 2: Mocking GetByName Method to throws var establishmentServiceMock = new Mock <EstablishmentService>(dbSettings); establishmentServiceMock.Setup(es => es.GetById(It.IsAny <string>())).ThrowsAsync(new InvalidOperationException()); var establishmentsControllerLocal = new EstablishmentsController(loggerWrapper, establishmentServiceMock.Object, controllerMessages); // 3: Call POST Action and Expects to throws await Assert.ThrowsAsync <InvalidOperationException>(async() => await establishmentsControllerLocal.Delete(id)); }
public EstablishmentControllerTests(DatabaseSettingsFixture dbFixture) { // 0: Setting wrapper for logger loggerWrapper = new LoggerWrapper <EstablishmentsController>(); // 1: Setting establishment service given db settings this.dbSettings = dbFixture.dbSettings; this.establishmentService = dbFixture.establishmentService; // 2: Get controller messages this.controllerMessages = GetControllerMessagesProperties(); // 3: Instantiate of Establishment Controller this.establishmentsController = new EstablishmentsController(loggerWrapper, establishmentService, controllerMessages); }
public async void Post_ThrowsException_Test() { // 0: Remove all establishments from database await establishmentService.RemoveAll(); // 1: Request body var requestBody = new EstablishmentRequest { Name = "Test 1", Type = "Alimentação" }; // 2: Mocking GetByName Method to throws var establishmentServiceMock = new Mock <EstablishmentService>(dbSettings); establishmentServiceMock.Setup(es => es.GetByName(It.IsAny <string>())).ThrowsAsync(new InvalidOperationException()); var establishmentsControllerLocal = new EstablishmentsController(loggerWrapper, establishmentServiceMock.Object, controllerMessages); // 3: Call POST Action and Expects to throws await Assert.ThrowsAsync <InvalidOperationException>(async() => await establishmentsControllerLocal.Post(requestBody)); }
public async void Put_Returns406_NotAcknowledged_Test() { // 0: Remove all establishments from database await establishmentService.RemoveAll(); // 1: Request body string id = "5dcaad2526235a471cfcccad"; var requestBody = new EstablishmentRequest { Name = "Test 1", Type = "Alimentação" }; // 2: Mocking GetById Method to return fake data var fakeEstablishment = new Establishment { Id = id, Name = requestBody.Name, Type = requestBody.Type }; var establishmentServiceMock = new Mock <EstablishmentService>(dbSettings); establishmentServiceMock.Setup(es => es.GetById(It.IsAny <string>())).ReturnsAsync(fakeEstablishment); var replaceOneResultWrapper = new ReplaceOneResultWrapper(); establishmentServiceMock.Setup(es => es.UpdateById(It.IsAny <string>(), It.IsAny <Establishment>())).ReturnsAsync(replaceOneResultWrapper); var establishmentsControllerLocal = new EstablishmentsController(loggerWrapper, establishmentServiceMock.Object, controllerMessages); var query = await establishmentsControllerLocal.Put(id, requestBody); var result = query.Result.GetType().GetProperty("Value").GetValue(query.Result); Type resultType = result.GetType(); Assert.Equal(406, (int)resultType.GetProperty("StatusCode").GetValue(result)); Assert.Equal(controllerMessages.CantUpdate, (string)resultType.GetProperty("Message").GetValue(result)); }