示例#1
0
        public async void Put_Returns404_EstablishmentNotFound_Test()
        {
            // 1: Request body
            string id          = "5dcaad2526235a471cfcccad";
            var    requestBody = new ReleaseRequest()
            {
                Date              = "05/05/2019",
                PaymentMethod     = PaymentMethod.Credito,
                EstablishmentName = "Test 1",
                Amount            = 55.55m
            };

            // 2: Mocking GetById to return a fake release object
            var releasesServiceMock = new Mock <ReleasesService>(dbSettings);

            releasesServiceMock.Setup(es => es.GetById(It.IsAny <string>())).ReturnsAsync(new Release()
            {
                Date              = requestBody.Date,
                PaymentMethod     = requestBody.PaymentMethod,
                EstablishmentName = requestBody.EstablishmentName,
                Amount            = requestBody.Amount,
                CreatedAt         = DateTime.Now
            });

            var releasesControllerLocal = new ReleasesController(loggerWrapper, releasesServiceMock.Object, establishmentService, controllerMessages);

            var query = await releasesControllerLocal.Put(id, requestBody);

            var  result     = query.Result.GetType().GetProperty("Value").GetValue(query.Result);
            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));
        }
示例#2
0
        public async void Put_InvalidId400_Test(string id)
        {
            // 1: Request body
            var requestBody = new ReleaseRequest()
            {
                Date              = "05/05/2019",
                PaymentMethod     = PaymentMethod.Credito,
                EstablishmentName = "Test 1",
                Amount            = 55.55m
            };

            // 2: Call PUT Action passing body request with an updated release
            var query = await releasesController.Put(id, requestBody);

            var  result     = query.Result.GetType().GetProperty("Value").GetValue(query.Result);
            Type resultType = result.GetType();

            Assert.Equal(400, (int)resultType.GetProperty("StatusCode").GetValue(result));
            Assert.Equal(controllerMessages.IncorretIdFormat, (string)resultType.GetProperty("Message").GetValue(result));
        }
示例#3
0
        public async void Put_Returns406_NotAcknowledged_Test()
        {
            // 1: Request body
            string id          = "5dcaad2526235a471cfcccad";
            var    requestBody = new ReleaseRequest()
            {
                Date              = "05/05/2019",
                PaymentMethod     = PaymentMethod.Credito,
                EstablishmentName = "Test 1",
                Amount            = 55.55m
            };

            // 2: Mocking GetById Method to return fake data
            var releasesServiceMock = new Mock <ReleasesService>(dbSettings);

            releasesServiceMock.Setup(es => es.GetById(It.IsAny <string>())).ReturnsAsync(new Release()
            {
                Date              = requestBody.Date,
                PaymentMethod     = requestBody.PaymentMethod,
                EstablishmentName = requestBody.EstablishmentName,
                Amount            = requestBody.Amount,
                CreatedAt         = DateTime.Now
            });

            var establishmentServiceMock = new Mock <EstablishmentService>(dbSettings);

            establishmentServiceMock.Setup(es => es.GetByName(It.IsAny <string>())).ReturnsAsync(new Establishment()
            {
                Name = "Test 1",
                Type = "Alimentação"
            });

            var replaceOneResultWrapper = new ReplaceOneResultWrapper();

            releasesServiceMock.Setup(es => es.UpdateById(It.IsAny <string>(), It.IsAny <Release>())).ReturnsAsync(replaceOneResultWrapper);

            var releasesControllerLocal = new ReleasesController(loggerWrapper, releasesServiceMock.Object, establishmentServiceMock.Object, controllerMessages);

            var query = await releasesControllerLocal.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));
        }
示例#4
0
        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);
        }
示例#5
0
        public async void Put_ThrowsException_Test()
        {
            // 1: Request body
            string id          = "5dcaad2526235a471cfcccad";
            var    requestBody = new ReleaseRequest()
            {
                Date              = "05/05/2019",
                PaymentMethod     = PaymentMethod.Credito,
                EstablishmentName = "Test 1",
                Amount            = 55.55m
            };

            // 2: Mocking GetByName Method to throws
            var releasesServiceMock = new Mock <ReleasesService>(dbSettings);

            releasesServiceMock.Setup(es => es.GetById(It.IsAny <string>())).ThrowsAsync(new InvalidOperationException());

            var releasesControllerLocal = new ReleasesController(loggerWrapper, releasesServiceMock.Object, establishmentService, controllerMessages);

            // 3: Call POST Action and Expects to throws
            await Assert.ThrowsAsync <InvalidOperationException>(async() => await releasesControllerLocal.Put(id, requestBody));
        }