public async void GetAddressByIdWithContactsAsync_Returns_One_Address_With_Related_Contacts(int id) { //get the first Address and its contacts var address = _addresses.FirstOrDefault <Address>(); address.Contacts = ContactsData.getTestContacts().Where(co => co.Address_Id == id) as ICollection <Contact>; var expectedDto = _mapper.Map <AddressWithContactsDTO>(address); //specify the mockRepo return _mockRepository.Setup(repo => repo.GetOneByWithRelatedDataAsync(ad => ad.Id == id, ad => ad.Contacts)).ReturnsAsync(address); //instantiate the controller, and call the method var controller = new AddressesController(_mockRepository.Object, _mapper, _logger); //Call the SUT method var actionResult = await controller.GetAddressByIdWithContactsAsync(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 actualDto = result.Value as AddressWithContactsDTO; //use FluentAssertions to compare Reference types actualDto.Should().BeEquivalentTo(expectedDto, options => options.ComparingByMembers <AddressDTO>()); }
[InlineData(4)] //Client with id 4 has related Contacts public async void GetClientByIdWithContactsAsync_Returns_One_Client_With_Related_Contacts(int id) { //get the Client with id 4 (that has related Contacts) and map it to its DTO var client = _clients.First <Client>(cl => cl.Id == id); client.Contacts = ContactsData.getTestContacts() as ICollection <Contact>; var expectedClientDto = _mapper.Map <ClientWithContactsDTO>(client); //specify the mockRepo return _mockRepository.Setup(repo => repo.GetOneByWithRelatedDataAsync(cl => cl.Id == id, cl => cl.Contacts)).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.GetClientByIdWithContactsAsync(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 ClientWithContactsDTO; //use FluentAssertions to compare Reference types actualClientDto.Should().BeEquivalentTo(expectedClientDto, options => options.ComparingByMembers <ClientWithContactsDTO>()); }
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 ContactsControllerUnitTests() { //get Billable Activities test data _contacts = ContactsData.getTestContacts(); //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 <ContactsController> >(); _logger = _loggerMock.Object; //Mock Repo initialization _mockRepository = new Mock <IGenericRepository <Contact> >(); //QueryStringParameters for paging parameters = new QueryStringParameters(); parameters.pageNumber = 1; parameters.pageSize = 10; }