Exemplo n.º 1
0
        public async void Can_Add_User()
        {
            // arrange
            // 1. First it's necessary to store the user data somehow
            var mockUserRepository = new Mock <IUserRepository>();

            mockUserRepository
            .Setup(repo => repo.Create(It.IsAny <User>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new CreateUserResponse(1, true)));

            // 2. We declare and instantiate the UseCase
            var useCase = new AddAsyncUserUseCase(mockUserRepository.Object);

            // 3. The output port is the way to pass response data from the UseCase to a Presenter for final preparation to deliver back to the UI/web page/api response etc.
            var mockOutputPort = new Mock <IOutputPort <AddUserResponse> >();

            mockOutputPort.Setup(outputPort => outputPort.Handle(It.IsAny <AddUserResponse>()));

            // act
            // 4. We need a model request to carry data into the use case from the upper layer (UI, Controller etc.)
            var response = await useCase.Handle(new AddUserRequest(0, "name", DateTime.Now, "password"), mockOutputPort.Object);

            // assert
            Assert.True(response);
        }
Exemplo n.º 2
0
        public async void Post_Returns_Ok_When_Update_User_By_Id_Use_Case_Succeeds()
        {
            // arrange
            var mockUserRepository = new Mock <IUserRepository>();

            mockUserRepository
            .Setup(repo => repo.Update("", It.IsAny <User>()))
            .Returns(Task.FromResult(new Core.Dto.GatewayResponses.Repositories.UpdateUserByIdResponse(1, true)));

            // fakes
            var outputAddUserPort        = new AddUserPresenter();
            var outputGetUsersPort       = new GetUsersPresenter();
            var outputGetUserByIdPort    = new GetUserByIdPresenter();
            var outputUpdateUserPort     = new UpdateUserByIdPresenter();
            var outputDeleteUserByIdPort = new DeleteUserByIdPresenter();

            var addUserUseCase        = new AddAsyncUserUseCase(mockUserRepository.Object);
            var getUsersUseCase       = new GetAsyncUsersUseCase(mockUserRepository.Object);
            var getUserByIdUseCase    = new GetAsyncUserByIdUseCase(mockUserRepository.Object);
            var updateUserByIdUseCase = new UpdateAsyncUserByIdUseCase(mockUserRepository.Object);
            var deleteUserByIdUseCase = new DeleteAsyncUserByIdUseCase(mockUserRepository.Object);

            var controller = new UserController(addUserUseCase, outputAddUserPort,
                                                getUserByIdUseCase, outputGetUserByIdPort,
                                                getUsersUseCase, outputGetUsersPort,
                                                updateUserByIdUseCase, outputUpdateUserPort,
                                                deleteUserByIdUseCase, outputDeleteUserByIdPort);

            // act
            var result = await controller.UpdateUserByIdAsync("userIdToUpdate", new UpdateUserRequest()
            {
                Updated_id = 999, Updated_name = "newName", Updated_birthdate = DateTime.Now, Updated_password = "******"
            });

            // assert
            var statusCode = ((ContentResult)result).StatusCode;

            Assert.True(statusCode.HasValue && statusCode.Value == (int)HttpStatusCode.OK);
        }
Exemplo n.º 3
0
        public async void Post_Returns_Ok_When_Add_User_Use_Case_Succeeds()
        {
            // arrange
            var mockUserRepository = new Mock <IUserRepository>();

            mockUserRepository
            .Setup(repo => repo.Create(It.IsAny <User>(), It.IsAny <string>()))
            .Returns(Task.FromResult(new CreateUserResponse(1, true)));

            // fakes
            var outputAddUserPort        = new AddUserPresenter();
            var outputGetUsersPort       = new GetUsersPresenter();
            var outputGetUserByIdPort    = new GetUserByIdPresenter();
            var outputUpdateUserPort     = new UpdateUserByIdPresenter();
            var outputDeleteUserByIdPort = new DeleteUserByIdPresenter();

            var addUserUseCase        = new AddAsyncUserUseCase(mockUserRepository.Object);
            var getUsersUseCase       = new GetAsyncUsersUseCase(mockUserRepository.Object);
            var getUserByIdUseCase    = new GetAsyncUserByIdUseCase(mockUserRepository.Object);
            var updateUserByIdUseCase = new UpdateAsyncUserByIdUseCase(mockUserRepository.Object);
            var deleteUserByIdUseCase = new DeleteAsyncUserByIdUseCase(mockUserRepository.Object);

            var controller = new UserController(addUserUseCase, outputAddUserPort,
                                                getUserByIdUseCase, outputGetUserByIdPort,
                                                getUsersUseCase, outputGetUsersPort,
                                                updateUserByIdUseCase, outputUpdateUserPort,
                                                deleteUserByIdUseCase, outputDeleteUserByIdPort);

            // act
            var result = await controller.AddUserAsync(new Models.Request.AddUserRequest());

            // assert
            var statusCode = ((ContentResult)result).StatusCode;

            Assert.True(statusCode.HasValue && statusCode.Value == (int)HttpStatusCode.OK);
        }