public void Create() { var address = new Address { Id = 6, StreetName = "Knasgatan", Number = 6, PostalCode = "66669", Area = "Knasköping", Country = "Knasland" }; _adressController.Create(address); _mockSetAddress.Verify(a => a.Add(address), Times.Once); _mockContext.Verify(a => a.SaveChanges(), Times.Once); }
public void Create_WhenNonAuthenticatedRequestForViewReceived_ThenRedirectToSignInViewActionReturned() { _mockAuthService .Setup(a => a.IsAuthenticated(_user)) .Returns(false); var controller = new AddressController(_mockAuthService.Object, _mockRepository.Object); var actual = controller.Create() as RedirectToActionResult; Assert.IsNotNull(actual); Assert.IsTrue(actual.ActionName == "SignIn"); Assert.IsTrue(actual.ControllerName == "Authentication"); }
public void Verify_Correct_Route_When_Calling_Create() { //Arrange var addressVM = new AddressVM() { ID = 1, Name = "test1", Line1 = "1 street", City = "City1", State = (State)1, Zip = 91234 }; //Act var result = (RedirectToRouteResult)objController.Create(addressVM); //Assert _addressServiceMock.Verify(m => m.AddRecord(It.IsAny <Address>()), Times.Once); Assert.Equal(Constant.Controller.Index, result.RouteValues[Constant.Controller.Action]); }
public void AddressCreateTest() { //setup var address = new Address(); var repo = new Mock <IGenericRepository <Address> >(); repo.Setup(x => x.Create(address)); _addressController = new AddressController(repo.Object); //execute _addressController.Create(address); //assert repo.Verify(x => x.Create(address), Times.Once); }
public void Create() { var memoryStore = new List <Address>(); Address addressStub = new Address { City = "Lviv" }; var mock = new Mock <IRepository <Address> >(); mock.Setup(repo => repo.GetAll()).Returns(memoryStore.AsQueryable()); mock.Setup(repo => repo.Create(It.IsAny <Address>())).Returns((Address address) => { address.Id = 1; memoryStore.Add(address); return(address); }); var factoryMock = new Mock <IRepositoryFactory>(); factoryMock.Setup(f => f.GetRepository <Address>()).Returns(mock.Object); var serviceMock = new Mock <ICleaningServiceServiceFactory>(); factoryMock.Setup(f => f.GetRepository <Address>()).Returns(mock.Object); _addressController = new AddressController(serviceMock.Object, factoryMock.Object); var emptyJson = _addressController.GetAll(); Assert.IsNotNull(emptyJson); var emptyStore = emptyJson.Value as List <Address>; Assert.IsNotNull(emptyStore); Assert.AreEqual(emptyStore.Count, 0); var json = _addressController.Create(addressStub); Assert.IsNotNull(json); var result = json.Value as Address; Assert.NotNull(result); Assert.AreEqual(result.Id, 1); Assert.AreEqual(result.City, addressStub.City); var notEmptyJson = _addressController.GetAll(); Assert.IsNotNull(notEmptyJson); var notEmptyStore = notEmptyJson.Value as List <Address>; Assert.IsNotNull(notEmptyStore); Assert.AreEqual(notEmptyStore.Count, 1); }
public async void Create_Errors() { AddressControllerMockFacade mock = new AddressControllerMockFacade(); var mockResponse = new Mock <CreateResponse <ApiAddressResponseModel> >(new FluentValidation.Results.ValidationResult()); var mockRecord = new ApiAddressResponseModel(); mockResponse.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiAddressRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiAddressResponseModel> >(mockResponse.Object)); AddressController controller = new AddressController(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 ApiAddressRequestModel()); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiAddressRequestModel>())); }
public void TestAddUserAddressWithoutProvinceOrCityOrAddressShouldFailed() { // Arrange var a = this.a; // Act try { var result = adCtl.Create(a); Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult)); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { Assert.IsNull(a.province); } catch (Exception ex) { Assert.Fail("got an exception: " + ex.GetBaseException().Message); } }
public async void Create_No_Errors() { AddressControllerMockFacade mock = new AddressControllerMockFacade(); var mockResponse = ValidationResponseFactory <ApiAddressServerResponseModel> .CreateResponse(null as ApiAddressServerResponseModel); mockResponse.SetRecord(new ApiAddressServerResponseModel()); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiAddressServerRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiAddressServerResponseModel> >(mockResponse)); AddressController controller = new AddressController(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 ApiAddressServerRequestModel()); response.Should().BeOfType <CreatedResult>(); (response as CreatedResult).StatusCode.Should().Be((int)HttpStatusCode.Created); var createResponse = (response as CreatedResult).Value as CreateResponse <ApiAddressServerResponseModel>; createResponse.Record.Should().NotBeNull(); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiAddressServerRequestModel>())); }
public void Create_WhenAuthenticatedRequestForViewReceived_ThenPartialViewReturned() { _mockAuthService .Setup(a => a.IsAuthenticated(_user)) .Returns(true); var controller = new AddressController(_mockAuthService.Object, _mockRepository.Object); controller.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = _user } }; var actual = controller.Create() as PartialViewResult; Assert.IsNotNull(actual); Assert.IsInstanceOf <PartialViewResult>(actual); Assert.That("_AddressModal", Is.EqualTo(actual.ViewName)); }