Example #1
0
        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);
        }
Example #2
0
        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());
        }
Example #3
0
        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));
        }
Example #4
0
        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);
        }
Example #5
0
        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));
        }
Example #6
0
        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));
        }