public async void Cadastrar_test_InternalServerError()
        {
            //Given
            var mocker             = new AutoMoqer();
            var baseControllerMock = mocker.Create <UsuarioController>();

            var faker = AutoFaker.Create();

            var param = new UsuarioParam()
            {
                Nome = "", Senha = ""
            };

            var responseTask = new Exception();

            var expectResponse = baseControllerMock.StatusCode(500);

            var service = mocker.GetMock <IUsuarioCommand>();

            service.Setup(r => r.Criar(param)).ThrowsAsync(responseTask).Verifiable();

            //When
            var result = await baseControllerMock.Cadastrar(param);

            //Then
            var comparison = new CompareLogic();

            service.Verify(mock => mock.Criar(param), Times.Once());
            Assert.True(((ObjectResult)result).StatusCode == expectResponse.StatusCode);
        }
示例#2
0
        public async void Cadastrar_test_BadRequest()
        {
            //Given
            var mocker             = new AutoMoqer();
            var baseControllerMock = mocker.Create <UsuarioController>();

            var faker = AutoFaker.Create();

            var param = new UsuarioParam()
            {
                Nome = "", Senha = ""
            };

            var response = false;

            var responseTask = Task.Factory.StartNew(() => response);

            var expectResponse = baseControllerMock.BadRequest("Usuário não cadastrado");

            var service = mocker.GetMock <IUsuarioCommand>();

            service.Setup(r => r.Criar(param)).Returns(responseTask).Verifiable();

            //When
            var result = await baseControllerMock.Cadastrar(param);

            //Then
            var comparison = new CompareLogic();

            service.Verify(mock => mock.Criar(param), Times.Once());
            Assert.True(comparison.Compare(result, expectResponse).AreEqual);
        }
        public async Task <IActionResult> Cadastrar([FromBody] UsuarioParam user)
        {
            try
            {
                await _usuarioCommand.Criar(user);

                return(Ok("Usuário cadastrado com sucesso!"));
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
示例#4
0
        public async Task <bool> Criar(UsuarioParam user)
        {
            var hasRole = Enum.TryParse <RoleEnum>(user.Role, true, out RoleEnum roleUser);

            if (!hasRole)
            {
                throw new Exception("Perfil não encontrado!");
            }


            UsuarioEntity u = new UsuarioEntity {
                Id = Guid.NewGuid(), Login = user.Login, Senha = user.Senha, Nome = user.Nome, DataCadastro = DateTime.Now, Role = roleUser, Latitude = user.Latitude, Longitude = user.Longitude
            };

            await _usuariosRepository.Inserir(u);

            return(true);
        }
        public async Task Criar(UsuarioParam user)
        {
            var hasRole = Enum.TryParse <RoleEnum>(user.Role, true, out RoleEnum roleUser);

            if (!hasRole)
            {
                throw new Exception("Perfil não encontrado!");
            }


            var usuario = user.ConvertTo <UsuarioEntity>();

            usuario.DataCadastro = DateTime.Now;

            usuario.Role = roleUser;
            usuario.Id   = Guid.NewGuid();

            await _usuariosRepository.Inserir(usuario);
        }