Exemple #1
0
        public async void Can_Delete_User_By_Id()
        {
            // 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 DeleteAsyncUserByIdUseCase(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 <DeleteUserByIdResponse> >();

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

            // 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 DeleteUserByIdRequest(1), mockOutputPort.Object);

            // assert
            Assert.True(response);
        }
Exemple #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);
        }
Exemple #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);
        }