Пример #1
0
        [InlineData(4)] //Client with id 4 has related Addresses
        public async void GetClientByIdWithAddressesAsync_Returns_One_Client_With_Related_Addresses(int id)
        {
            //get the Client with id 4 (that has related Addresses) and map it to its DTO
            var client = _clients.First <Client>(cl => cl.Id == id);

            client.Addresses = AddressesData.getTestAddresses() as ICollection <Address>;
            var expectedClientDto = _mapper.Map <ClientWithAddressesDTO>(client);

            //specify the mockRepo return
            _mockRepository.Setup(repo => repo.GetOneByWithRelatedDataAsync(cl => cl.Id == id, cl => cl.Addresses)).ReturnsAsync(client);

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

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

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

            //convert ActionResult to OkObjectResult to get its Value: a Client type
            var result = Assert.IsType <OkObjectResult>(actionResult.Result);
            //get the ObjectResult.Value
            var actualClientDto = result.Value as ClientWithAddressesDTO;

            //use FluentAssertions to compare Reference types
            actualClientDto.Should().BeEquivalentTo(expectedClientDto, options => options.ComparingByMembers <ClientWithAddressesDTO>());
        }
        public async void GetContactByIdWithDetailsAsync_Returns_One_Contact_With_Related_Addresses(int id)
        {
            //get the first Contact and its address
            var contact = _contacts.FirstOrDefault <Contact>();

            contact.Address = AddressesData.getTestAddresses().First(ad => ad.Client_Id == id);
            var expectedContactDto = _mapper.Map <ContactWithAddressDTO>(contact);

            //specify the mockRepo return
            _mockRepository.Setup(repo => repo.GetOneByWithRelatedDataAsync(co => co.Id == id, co => co.Address)).ReturnsAsync(contact);

            //instantiate the controller, and call the method
            var controller = new ContactsController(_mockRepository.Object, _mapper, _logger);

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

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

            //convert ActionResult to OkObjectResult to get its Value
            var result = Assert.IsType <OkObjectResult>(actionResult.Result);
            //get the ObjectResult.Value
            var actualContactDto = result.Value as ContactWithAddressDTO;

            //use FluentAssertions to compare Reference types
            actualContactDto.Should().BeEquivalentTo(expectedContactDto, options => options.ComparingByMembers <ContactDTO>());
        }
Пример #3
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();
        }
        public AddressesControllerUnitTests()
        {
            //get Billable Activities test data
            _addresses = AddressesData.getTestAddresses();

            //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 <AddressesController> >();

            _logger = _loggerMock.Object;

            //Mock Repo initialization
            _mockRepository = new Mock <IGenericRepository <Address> >();

            //QueryStringParameters for paging
            parameters            = new QueryStringParameters();
            parameters.pageNumber = 1;
            parameters.pageSize   = 10;
        }