Example #1
0
        public async void GetAllClientsAsync_Returns_All_Clients()
        {
            //map Clients test data to DTOs
            var _clients     = ClientsData.getTestClients();
            var expectedDTOs = _mapper.Map <IEnumerable <ClientDTO> >(_clients);

            //specify the mockRepo return
            _mockRepository.Setup(repo => repo.GetAllPagedAsync(cl => cl.Id, parameters)).ReturnsAsync(_clients);

            //instantiate the controller
            var controller = new ClientsController(_mockRepository.Object, _mapper, _logger);

            //Call the SUT method
            var result = await controller.GetAllClientsAsync(parameters);

            //Assert the result
            Assert.NotNull(result);
            var actionResult             = Assert.IsType <ActionResult <IEnumerable <ClientDTO> > >(result);
            var objResult                = Assert.IsType <OkObjectResult>(result.Result);
            var actualClients            = objResult.Value;
            IEnumerable <ClientDTO> dtos = _mapper.Map <IEnumerable <ClientDTO> >(actualClients);

            //use FluentAssertions to compare Collections of Reference types
            dtos.Should().BeEquivalentTo(expectedDTOs, options => options.ComparingByMembers <ClientDTO>());
        }
Example #2
0
        public async void DeleteClientAsync_Deletes_One_Client_And_Returns_Number_Of_Deletions(int id)
        {
            //declare a Client
            var       expectedClient = ClientsData.getTestClients().First(cl => cl.Id == id);
            ClientDTO expectedDTO    = _mapper.Map <ClientDTO>(expectedClient);

            //set repo return for getting the object to delete
            _mockRepository.Setup(repo => repo.GetOneByAsync(cl => cl.Id == id))
            .ReturnsAsync(expectedClient);

            //set mockRepo return for Delete action
            _mockRepository.Setup(repo => repo.DeleteTAsync(expectedClient)).ReturnsAsync(1);

            //instantiate the controller, passing the repo object
            var controller = new ClientsController(_mockRepository.Object, _mapper, _logger);

            //Call the controller method
            var actionResult = await controller.DeleteClientAsync(id);

            //Get the int result
            var okObjectResult = actionResult.Result as OkObjectResult;
            var statusCode     = okObjectResult.StatusCode;
            int actualDeleted  = (int)okObjectResult.Value;

            //Assert the result
            Assert.NotNull(actionResult);

            //Validate StatusCode
            Assert.Equal(200, statusCode);

            //Validate the number of BAs deleted
            Assert.Equal(1, actualDeleted);
        }
Example #3
0
        public async void UpdateClientAsync_Updates_One_Client_Returns_200_And_Client_Updated(int id)
        {
            //declare a Client
            var       expectedClient = ClientsData.getTestClients().First <Client>(cl => cl.Id == id);
            ClientDTO expectedDTO    = _mapper.Map <ClientDTO>(expectedClient);

            //set repo return for getting the object to update
            _mockRepository.Setup(repo => repo.GetOneByAsync(cl => cl.Id == expectedClient.Id))
            .ReturnsAsync(expectedClient);

            //set mockRepo return for Update action
            _mockRepository.Setup(repo => repo.UpdateTAsync(expectedClient)).ReturnsAsync(1);

            //instantiate the controller, passing the repo object
            var controller = new ClientsController(_mockRepository.Object, _mapper, _logger);

            //Call the SUT method - returns ActionResult<Client> type
            var actionResult = await controller.UpdateClientAsync(expectedClient.Id, expectedClient);

            //Get the int result from the posted ActionResult
            var       okObjectResult = actionResult.Result as OkObjectResult;
            var       statusCode     = okObjectResult.StatusCode;
            ClientDTO actualDTO      = okObjectResult.Value as ClientDTO;

            //Assert the result
            Assert.NotNull(actionResult);

            //Validate StatusCode
            Assert.Equal(200, statusCode);

            //Validate the actual Client
            actualDTO.Should().BeEquivalentTo(expectedDTO, options => options.ComparingByMembers <ClientDTO>());
        }
Example #4
0
        public static void SeedDB(CMTestsDbContext context)
        {
            context.AddRange(AddressesData.getTestAddresses());
            context.AddRange(BillableActivitiesData.getTestBillableActivities());
            context.AddRange(ClientsData.getTestClients());
            context.AddRange(ContactsData.getTestContacts());
            context.AddRange(EmployeesData.getTestEmployees());
            context.AddRange(EmployeeTypesData.getTestEmployeeTypes());
            context.AddRange(LegalCasesData.getTestLegalCases());

            context.SaveChanges();
        }
Example #5
0
        public async Task GetAllClientsAsync_Returns_All_Clients_Paged(string url, int pageNumber, int pageSize)
        {
            //Arrange
            //create test HttpClient
            var client = _factory.CreateClient();

            //get test Clients
            IEnumerable <Client> expectedClients = ClientsData.getTestClients();

            //Act
            string uri          = $"{url}?pageNumber={pageNumber}&pageSize={pageSize}";
            var    httpResponse = await client.GetAsync(uri);

            //process Http response
            string serializedResponse = httpResponse.Content.ReadAsStringAsync().Result;
            var    actualClients      = JsonConvert.DeserializeObject <IEnumerable <Client> >(serializedResponse);

            //Assert
            Assert.True(httpResponse.IsSuccessStatusCode);

            //use FluentAssertions to compare Collections of Reference types
            actualClients.Should().BeEquivalentTo(expectedClients, options => options.ComparingByMembers <Client>());
        }
Example #6
0
        public ClientsControllerTests()
        {
            //get Clients test data
            _clients = ClientsData.getTestClients();

            //AutoMapper Configuration
            var profiles      = new AutoMapperProfiles();
            var configuration = new MapperConfiguration(config => config.AddProfile(profiles));

            _mapper = new Mapper(configuration);

            //Configure Logger Mock
            var _loggerMock = new Mock <ILogger <ClientsController> >();

            _logger = _loggerMock.Object;

            //Mock Repository initialization
            _mockRepository = new Mock <IGenericRepository <Client> >();

            //QueryStringParameters for paging
            parameters            = new QueryStringParameters();
            parameters.pageNumber = 1;
            parameters.pageSize   = 10;
        }
Example #7
0
        public async void AddClientAsync_Creates_One_Client_Returns_201_And_Client_Created(int id)
        {
            //get a Client and set expected DTO
            var       expectedClient = ClientsData.getTestClients().First <Client>(cl => cl.Id == id);
            ClientDTO expectedDTO    = _mapper.Map <ClientDTO>(expectedClient);

            //set mockRepo return for Add action
            _mockRepository.Setup(repo => repo.AddTAsync(expectedClient)).ReturnsAsync(1);

            //set repo return for getting the newly created object
            _mockRepository.Setup(repo => repo.GetOneByAsync(cl =>
                                                             cl.Name == expectedClient.Name &&
                                                             cl.Description == expectedClient.Description &&
                                                             cl.Website == expectedClient.Website))
            .ReturnsAsync(expectedClient);

            //instantiate the controller, passing the repo object
            var controller = new ClientsController(_mockRepository.Object, _mapper, _logger);

            //Call the SUT method - returns ActionResult<Client> type
            var actionResult = await controller.AddClientAsync(expectedClient);

            //Get the int result from the posted ActionResult
            var       createdResult = actionResult.Result as CreatedResult;
            var       statusCode    = createdResult.StatusCode;
            ClientDTO actualDTO     = createdResult.Value as ClientDTO;

            //Assert the result
            Assert.NotNull(actionResult);

            //Validate the return is 1 Client created
            Assert.Equal(201, statusCode);

            //Validate the actual Client
            actualDTO.Should().BeEquivalentTo(expectedDTO, options => options.ComparingByMembers <ClientDTO>());
        }