public async void BulkInsert_No_Errors() { FamilyControllerMockFacade mock = new FamilyControllerMockFacade(); var mockResponse = new CreateResponse <ApiFamilyResponseModel>(new FluentValidation.Results.ValidationResult()); mockResponse.SetRecord(new ApiFamilyResponseModel()); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiFamilyRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiFamilyResponseModel> >(mockResponse)); FamilyController controller = new FamilyController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); var records = new List <ApiFamilyRequestModel>(); records.Add(new ApiFamilyRequestModel()); IActionResult response = await controller.BulkInsert(records); response.Should().BeOfType <OkObjectResult>(); (response as OkObjectResult).StatusCode.Should().Be((int)HttpStatusCode.OK); var result = (response as OkObjectResult).Value as List <ApiFamilyResponseModel>; result.Should().NotBeEmpty(); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiFamilyRequestModel>())); }
public async void Patch_No_Errors() { FamilyControllerMockFacade mock = new FamilyControllerMockFacade(); var mockResult = new Mock <UpdateResponse <ApiFamilyResponseModel> >(); mockResult.SetupGet(x => x.Success).Returns(true); mock.ServiceMock.Setup(x => x.Update(It.IsAny <int>(), It.IsAny <ApiFamilyRequestModel>())) .Callback <int, ApiFamilyRequestModel>( (id, model) => model.Notes.Should().Be("A") ) .Returns(Task.FromResult <UpdateResponse <ApiFamilyResponseModel> >(mockResult.Object)); mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <ApiFamilyResponseModel>(new ApiFamilyResponseModel())); FamilyController controller = new FamilyController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, new ApiFamilyModelMapper()); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); var patch = new JsonPatchDocument <ApiFamilyRequestModel>(); patch.Replace(x => x.Notes, "A"); IActionResult response = await controller.Patch(default(int), patch); response.Should().BeOfType <OkObjectResult>(); (response as OkObjectResult).StatusCode.Should().Be((int)HttpStatusCode.OK); mock.ServiceMock.Verify(x => x.Update(It.IsAny <int>(), It.IsAny <ApiFamilyRequestModel>())); }
public void AddFamilyTest() { var mediator = new Mock <IMediator>(); mediator .Setup(m => m.Send(It.IsAny <AddGroupFamilyCommand>(), It.IsAny <CancellationToken>())) .ReturnsAsync(ResponseFamilyErrorEnum.Ok) //<-- return Task to allow await to continue .Verifiable("Notification was not sent."); var identity = new GenericIdentity("*****@*****.**"); Thread.CurrentPrincipal = new GenericPrincipal(identity, null); FamilyController controller = new FamilyController(mediator.Object, null); controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); var task = controller.AddFamily(new Models.AddFamilyBindingModels() { FamilyName = "Silva" }); task.Wait(); var actionResult = task.Result.ExecuteAsync(new CancellationToken()).Result; ModelResult model; var contentResult = actionResult.TryGetContentValue <ModelResult>(out model); Assert.IsTrue(actionResult.IsSuccessStatusCode, model.Message); }
public void Get_WhenCalled_ReturnNoItems() { mock.Setup(repo => repo.GetAllItems(null)).Returns(emptyListItems.AsQueryable()); var controller = new FamilyController(mockChild.Object, mock.Object); var result = controller.Get(); Assert.IsAssignableFrom <IEnumerable <Family> >(result); Assert.Equal(emptyListItems.Count, result.ToList().Count); }
public void Create_NullObject_ShouldReturnBadRequest() { mock.Setup(repo => repo.GetAllItems(null)).Returns(listItems.AsQueryable()); mock.Setup(repo => repo.Create(null)); var controller = new FamilyController(mockChild.Object, mock.Object); var result = controller.Create(null); Assert.IsType <BadRequestResult>(result.Result); }
public async void PostFamilyTest() { // Arrange var profile = new MammalAPI.Configuration.Mapper(); var configuration = new MapperConfiguration(cfg => cfg.AddProfile(profile)); IMapper mapper = new Mapper(configuration); //Mock Repo var familyRepoMock = new Mock <IFamilyRepository>(); familyRepoMock.Setup(r => r.Add <Family>(It.IsAny <Family>())); familyRepoMock.Setup(r => r.GetAllFamilies(It.IsAny <Boolean>())).Returns(Task.FromResult(new Family[1])); familyRepoMock.Setup(r => r.Save()).Returns(Task.FromResult(true)); //Mock IActionDescriptorCollectionProvider var actions = new List <ActionDescriptor> { new ActionDescriptor { AttributeRouteInfo = new AttributeRouteInfo() { Template = "/test", }, RouteValues = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase) { { "action", "Test" }, { "controller", "Test" }, }, }, }; var mockDescriptorProvider = new Mock <IActionDescriptorCollectionProvider>(); mockDescriptorProvider.Setup(m => m.ActionDescriptors).Returns(new ActionDescriptorCollection(actions, 0)); //Setup new controller based on mocks var controller = new FamilyController(familyRepoMock.Object, mapper, mockDescriptorProvider.Object); //Create new DTO var dto = new FamilyDTO { Name = "test", FamilyID = 1 }; // Act var result = await controller.PostFamily(dto); // Assert var r = result.Result as CreatedResult; var dtoResult = (FamilyDTO)r.Value; Assert.Equal("test", dtoResult.Name); }
public void GetById_ValidId_ShouldReturnValidObject() { mock.Setup(repo => repo.GetAllItems(null)).Returns(listItems.AsQueryable()); mock.Setup(repo => repo.GetItem(1, null)).Returns(listItems[0]); var controller = new FamilyController(mockChild.Object, mock.Object); var result = controller.GetById(1); Assert.IsAssignableFrom <ActionResult <Family> >(result); Assert.Equal(listItems[0].Id, result.Value.Id); }
public void GetById_InvalidId_ShouldReturnNotFoundResult() { mock.Setup(repo => repo.GetAllItems(null)).Returns(listItems.AsQueryable()); mock.Setup(repo => repo.GetItem(4, null)).Returns(listItems[0]); var controller = new FamilyController(mockChild.Object, mock.Object); var result = controller.GetById(4); Assert.IsAssignableFrom <ActionResult <Family> >(result); Assert.IsType <NotFoundResult>(result.Result); }
public void Controller_FamilyController_Update_Default_Should_Pass() { // Arrange FamilyController controller = new FamilyController(); // Act ViewResult result = controller.Update() as ViewResult; // Assert Assert.IsNotNull(result); }
public void GetByParentId_ValidId_ShouldReturnValidObject() { listItems[0].Offeringes = childListItems; mock.Setup(repo => repo.GetAllItems(null)).Returns(listItems.AsQueryable()); mockChild.Setup(repo => repo.GetItemsByParentId(1, null)).Returns(childListItems.AsQueryable()); var controller = new FamilyController(mockChild.Object, mock.Object); var result = controller.GetOfferingsByIdFamily(1); Assert.IsAssignableFrom <ActionResult <IEnumerable <Offering> > >(result); Assert.Equal(listItems[0].Offeringes.Count, result.Value.ToList().Count); }
public void GetByParentId_InvalidId_ShouldReturnNotFoundResult() { listItems[0].Offeringes = childListItems; mock.Setup(repo => repo.GetAllItems(null)).Returns(listItems.AsQueryable()); mockChild.Setup(repo => repo.GetItemsByParentId(4, null)).Returns(childListItems.AsQueryable()); var controller = new FamilyController(mockChild.Object, mock.Object); var result = controller.GetOfferingsByIdFamily(4); Assert.IsAssignableFrom <ActionResult <IEnumerable <Offering> > >(result); Assert.IsType <NotFoundResult>(result.Result); }
public void Controller_FamilyController_Update_Post_Default_Should_Pass() { // Arrange FamilyController controller = new FamilyController(); var data = new PersonModel(); // Act ViewResult result = controller.Update(data) as ViewResult; // Assert Assert.IsNotNull(result); }
public async void GetAllFamilies_FetchListOfAllFamilies_LengthOfTwoExpected() { // Arrange var profile = new MammalAPI.Configuration.Mapper(); var configuration = new MapperConfiguration(cfg => cfg.AddProfile(profile)); IMapper mapper = new Mapper(configuration); //Mock context var testFamilies = GetTestFamilies(); var contextMock = new Mock <DBContext>(); contextMock.Setup(f => f.Families).ReturnsDbSet(testFamilies); //Mock Repo var logger = Mock.Of <ILogger <FamilyRepository> >(); var familyRepoMock = new FamilyRepository(contextMock.Object, logger); //Mock IActionDescriptorCollectionProvider var actions = new List <ActionDescriptor> { new ActionDescriptor { AttributeRouteInfo = new AttributeRouteInfo() { Template = "/test", }, RouteValues = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase) { { "action", "Test" }, { "controller", "Test" }, }, } }; var mockDescriptorProvider = new Mock <IActionDescriptorCollectionProvider>(); mockDescriptorProvider.Setup(m => m.ActionDescriptors).Returns(new ActionDescriptorCollection(actions, 0)); //Setup new controller based on mocks var controller = new FamilyController(familyRepoMock, mapper, mockDescriptorProvider.Object); //Act var result = await controller.GetAllFamilies(false); var contentResult = result as OkObjectResult; FamilyDTO[] dto = (FamilyDTO[])contentResult.Value; //Assert Assert.Equal(2, dto.Length); }
public void Create_ValidObject_ShouldReturnValidObjectAndCreatedAtObjectResult() { var fam3 = new Family { Id = 3, Name = "name3", Offeringes = null }; mock.Setup(repo => repo.GetAllItems(null)).Returns(listItems.AsQueryable()); mock.Setup(repo => repo.Create(fam3)); var controller = new FamilyController(mockChild.Object, mock.Object); var result = controller.Create(fam3); Assert.IsAssignableFrom <ActionResult <Family> >(result); Assert.IsType <CreatedAtActionResult>(result.Result); }
public void Delete_NotExistId_ShouldReturnNotFoundResult() { var fam3 = new Family { Id = 3, Name = "name3", Offeringes = null }; mock.Setup(repo => repo.GetAllItems(null)).Returns(listItems.AsQueryable()); mock.Setup(repo => repo.Delete(fam3)); var controller = new FamilyController(mockChild.Object, mock.Object); var result = controller.Delete(3); Assert.IsAssignableFrom <ActionResult <Family> >(result); Assert.IsType <NotFoundResult>(result.Result); }
public void Delete_ValidId_ShouldReturnDeletedObject() { var fam3 = new Family { Id = 1, Name = "Data center", Offeringes = null }; mock.Setup(repo => repo.GetAllItems(null)).Returns(listItems.AsQueryable()); mock.Setup(repo => repo.Delete(fam3)); var controller = new FamilyController(mockChild.Object, mock.Object); var result = controller.Delete(1); Assert.IsAssignableFrom <ActionResult <Family> >(result); Assert.Equal(fam3.Id, result.Value.Id); }
public async void Get_Not_Exists() { FamilyControllerMockFacade mock = new FamilyControllerMockFacade(); mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <ApiFamilyResponseModel>(null)); FamilyController controller = new FamilyController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Get(default(int)); response.Should().BeOfType <StatusCodeResult>(); (response as StatusCodeResult).StatusCode.Should().Be((int)HttpStatusCode.NotFound); mock.ServiceMock.Verify(x => x.Get(It.IsAny <int>())); }
public async Task IndexPOST_ReturnsBadRequestResult_WhenModelStateIsInvalid() { var mockService = new Mock <IPatientViewModelService>(); mockService.Setup(service => service.UpdatePatientWithFamily(It.IsAny <FamilyPatientViewModel>())); var controller = new FamilyController(mockService.Object); controller.ModelState.AddModelError("Name", "Required"); var familyViewModel = new FamilyViewModel(); var result = await controller.Index(familyViewModel); var badRequestResult = Assert.IsType <BadRequestObjectResult>(result); Assert.IsType <SerializableError>(badRequestResult.Value); }
public async Task Index_ReturnsViewResultWithFamilyViewModel_ShouldShowFamily() { // Arrange int testPatientId = 2; var mockService = new Mock <IPatientViewModelService>(); mockService.Setup(service => service.GetPatientWithFamily(testPatientId)) .Returns(Task.FromResult(It.IsAny <FamilyPatientViewModel>())); var controller = new FamilyController(mockService.Object); // Act var result = await controller.Index(testPatientId); // Assert var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsType <FamilyViewModel>(viewResult.ViewData.Model); }
public async void Delete_Errors() { FamilyControllerMockFacade mock = new FamilyControllerMockFacade(); var mockResult = new Mock <ActionResponse>(); mockResult.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.FromResult <ActionResponse>(mockResult.Object)); FamilyController controller = new FamilyController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Delete(default(int)); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Delete(It.IsAny <int>())); }
public async void Get_Exists() { FamilyControllerMockFacade mock = new FamilyControllerMockFacade(); mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult(new ApiFamilyServerResponseModel())); FamilyController controller = new FamilyController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Get(default(int)); response.Should().BeOfType <OkObjectResult>(); (response as OkObjectResult).StatusCode.Should().Be((int)HttpStatusCode.OK); var record = (response as OkObjectResult).Value as ApiFamilyServerResponseModel; record.Should().NotBeNull(); mock.ServiceMock.Verify(x => x.Get(It.IsAny <int>())); }
public async void All_Not_Exists() { FamilyControllerMockFacade mock = new FamilyControllerMockFacade(); mock.ServiceMock.Setup(x => x.All(It.IsAny <int>(), It.IsAny <int>())).Returns(Task.FromResult <List <ApiFamilyResponseModel> >(new List <ApiFamilyResponseModel>())); FamilyController controller = new FamilyController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.All(1000, 0); response.Should().BeOfType <OkObjectResult>(); (response as OkObjectResult).StatusCode.Should().Be((int)HttpStatusCode.OK); var items = (response as OkObjectResult).Value as List <ApiFamilyResponseModel>; items.Should().BeEmpty(); mock.ServiceMock.Verify(x => x.All(It.IsAny <int>(), It.IsAny <int>())); }
public async void Update_NotFound() { FamilyControllerMockFacade mock = new FamilyControllerMockFacade(); var mockResult = new Mock <UpdateResponse <ApiFamilyResponseModel> >(); mockResult.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Update(It.IsAny <int>(), It.IsAny <ApiFamilyRequestModel>())).Returns(Task.FromResult <UpdateResponse <ApiFamilyResponseModel> >(mockResult.Object)); mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <ApiFamilyResponseModel>(null)); FamilyController controller = new FamilyController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, new ApiFamilyModelMapper()); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Update(default(int), new ApiFamilyRequestModel()); response.Should().BeOfType <StatusCodeResult>(); (response as StatusCodeResult).StatusCode.Should().Be((int)HttpStatusCode.NotFound); mock.ServiceMock.Verify(x => x.Get(It.IsAny <int>())); }
public async void Patch_Record_Not_Found() { FamilyControllerMockFacade mock = new FamilyControllerMockFacade(); var mockResult = new Mock <ActionResponse>(); mock.ServiceMock.Setup(x => x.Get(It.IsAny <int>())).Returns(Task.FromResult <ApiFamilyResponseModel>(null)); FamilyController controller = new FamilyController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); var patch = new JsonPatchDocument <ApiFamilyRequestModel>(); patch.Replace(x => x.Notes, "A"); IActionResult response = await controller.Patch(default(int), patch); response.Should().BeOfType <StatusCodeResult>(); (response as StatusCodeResult).StatusCode.Should().Be((int)HttpStatusCode.NotFound); mock.ServiceMock.Verify(x => x.Get(It.IsAny <int>())); }
private void bunifuThinButton21_Click(object sender, EventArgs e) { if (textBox1.Text != "") { FamilyController _family = new FamilyController(); _family.insert(new Family() { family = textBox1.Text }); MessageBox.Show("Cadastrado com sucesso"); if (activeContructor == FamilyRestViewConstructor) { frmFamilyRest.fillData(); } Close(); } else { MessageBox.Show("Preencha o campo correctamente"); } }
public async void Create_No_Errors() { FamilyControllerMockFacade mock = new FamilyControllerMockFacade(); var mockResponse = ValidationResponseFactory <ApiFamilyServerResponseModel> .CreateResponse(null as ApiFamilyServerResponseModel); mockResponse.SetRecord(new ApiFamilyServerResponseModel()); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiFamilyServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiFamilyServerResponseModel> >(mockResponse)); FamilyController controller = new FamilyController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); IActionResult response = await controller.Create(new ApiFamilyServerRequestModel()); response.Should().BeOfType <CreatedResult>(); (response as CreatedResult).StatusCode.Should().Be((int)HttpStatusCode.Created); var createResponse = (response as CreatedResult).Value as CreateResponse <ApiFamilyServerResponseModel>; createResponse.Record.Should().NotBeNull(); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiFamilyServerRequestModel>())); }
public async Task GetFromFormSiblings_AddSisterViewModel_WhenSiblingTypeSister() { var mockService = new Mock <IPatientViewModelService>(); mockService.Setup(service => service.UpdatePatientWithFamily(It.IsAny <FamilyPatientViewModel>())) .Returns(Task.CompletedTask) .Verifiable(); var controller = new FamilyController(mockService.Object); // Give the controller an HttpContext. controller.ControllerContext.HttpContext = new DefaultHttpContext(); // Request is not null anymore. controller.Request.Form = GetRequestFormCollection(); var familyViewModel = GetFamilyViewModel(); var result = await controller.Index(familyViewModel); Assert.IsAssignableFrom <SisterViewModel>(familyViewModel.Patient.Siblings.FirstOrDefault()); Assert.Single(familyViewModel.Patient.Siblings); Assert.Single(familyViewModel.Patient.Sisters); Assert.Empty(familyViewModel.Patient.Brothers); }
public async Task IndexPOST_ReturnsARedirectAndUpdatePatientFamily_WhenModelStateIsValid() { var mockService = new Mock <IPatientViewModelService>(); mockService.Setup(service => service.UpdatePatientWithFamily(It.IsAny <FamilyPatientViewModel>())) .Returns(Task.CompletedTask) .Verifiable(); var controller = new FamilyController(mockService.Object); // Give the controller an HttpContext. controller.ControllerContext.HttpContext = new DefaultHttpContext(); // Request is not null anymore. controller.Request.Form = GetRequestFormCollection(); var familyViewModel = GetFamilyViewModel(); var result = await controller.Index(familyViewModel); var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Index", redirectToActionResult.ActionName); mockService.Verify(); }
public async void BulkInsert_Errors() { FamilyControllerMockFacade mock = new FamilyControllerMockFacade(); var mockResponse = new Mock <CreateResponse <ApiFamilyServerResponseModel> >(null as ApiFamilyServerResponseModel); mockResponse.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiFamilyServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiFamilyServerResponseModel> >(mockResponse.Object)); FamilyController controller = new FamilyController(mock.ApiSettingsMoc.Object, mock.LoggerMock.Object, mock.TransactionCoordinatorMock.Object, mock.ServiceMock.Object, mock.ModelMapperMock.Object); controller.ControllerContext = new ControllerContext(); controller.ControllerContext.HttpContext = new DefaultHttpContext(); var records = new List <ApiFamilyServerRequestModel>(); records.Add(new ApiFamilyServerRequestModel()); IActionResult response = await controller.BulkInsert(records); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiFamilyServerRequestModel>())); }