public void CreateLoadsCategories() { // act ViewResult actual = (ViewResult)controller.Create(); // assert Assert.IsNotNull(actual.ViewBag.CategoryId); }
public void PostsController_Create_Get() { var viewResult = _postsController.Create() as ViewResult; Assert.IsTrue(viewResult != null); var categories = (viewResult.Model as PostViewModel).Categories.ToList(); Assert.IsTrue(categories.SequenceEqual(TestsHelper.GetCategories(), new SelectListItemComparer())); }
public async Task CreatePost_ModelStateIsNotValid_ReturnViewResult() { _unitOfWork .Setup(x => x.PostCategoryRepository.GetAll()) .Returns(new Mock <IQueryable <PostCategory> >().Object); _controller.ModelState.AddModelError(nameof(PostFormViewModel.Title), "Error"); var result = await _controller.Create(new PostFormViewModel()); result.Should().BeOfType <ViewResult>(); }
public void Post_Create_With_Error() { _mockUserContext.Setup(x => x.HasGroupPermission(It.IsAny <string>(), It.IsAny <string>())).Returns(true); _controller.ModelState.AddModelError("Error", "Error"); var result = _controller.Create(new PostCreateInput()); Assert.IsInstanceOf <JsonResult>(result); var jsonResult = result as JsonResult; Assert.IsNotNull(jsonResult); Assert.AreEqual(jsonResult.Data.ToString().ToLower(), "Failure".ToLower()); }
public void CreateTest() { //Arrange var mockControllerContext = new Mock <ControllerContext>(); var mockSession = new Mock <HttpSessionStateBase>(); mockSession.SetupGet(s => s["RoleId"]).Returns("2"); mockControllerContext.Setup(p => p.HttpContext.Session).Returns(mockSession.Object); PostsController postsController = new PostsController(); postsController.ControllerContext = mockControllerContext.Object; string expected = null; Post post = new Post(); post.PostId = 001; post.Title = "TestTitle"; post.Text = "TestTest"; string image = "C:\\Users\\taalam\\Documents\\Project\\Food\\Food\\ShopBridge.Test\\Contents\\images\\posts\\1.jpg"; post.Date = DateTime.Now; post.UserId = 1; post.Price = 100; //Act var result = postsController.Create() as ViewResult; //Assert Assert.AreEqual(result, expected); }
public async void CreatePost() { var post = new Post { Id = 1, Author = "author1", Title = "Post1", Text = "post 1 content" }; //setup var mockPostRepository = new Mock <IPostRepository>(); mockPostRepository.Setup(m => m.CreatePost(post)) .ReturnsAsync(1); //act var controller = new PostsController(mockPostRepository.Object); controller.ControllerContext.HttpContext = new DefaultHttpContext(); var result = await controller.Create(post); var viewResult = result as RedirectToActionResult; //Assert Assert.True(viewResult is RedirectToActionResult); Assert.Equal("MyPosts", viewResult.ActionName); }
public void MuestraUnaVistaParaRegistrarUnPost() { PostsController controller = new PostsController(null); var view = controller.Create() as ViewResult; Assert.AreEqual(String.Empty, view.ViewName); }
public async Task Posts_CreateAsync() { _mapper.Map <Post>(Arg.Any <PostViewModel>()).Returns(new Post()); await _controller.Create(Arg.Any <PostViewModel>()); _claimService.Received().GetClaimFromHttp(); await _userService.Received().GetUserByClaimAsync(Arg.Any <Claim>()); _postService.Received().InsertPost(Arg.Any <Post>()); }
public void GetSingularPostReturns() { Post post = new Post("post", "content"); post = (((controller.Create(blog.ID, post)) as ObjectResult).Value as Post); Assert.NotEqual(post.ID, 0); post = controller.GetPost(blog.ID, post.ID).Value; Assert.Equal(post.Title, "post"); }
public async Task PostsController_CreatesPost() { // Arrange createUser(); // Act using (var context = new ApplicationDbContext(inMemoryDatabaseHelper.Options)) { // Act var user = context.Users.Single(); var userRepoMock = new Mock <IUserRepository>(); userRepoMock.Setup(repo => repo.GetUserAsync(ClaimsPrincipal.Current)) .Returns(Task.FromResult(user)); var postsController = new PostsController(context, userRepoMock.Object, new Repository <Post>(context) ); Assert.That((await userRepoMock.Object.GetUserAsync(ClaimsPrincipal.Current)).Id, Is.EqualTo(user.Id)); var result = await postsController.Create(new Post() { Title = "title", Content = "content", OriginalPoster = user }); // Assert Assert.IsInstanceOf <RedirectToActionResult>(result); var redirectToActionResult = result as RedirectToActionResult; Assert.Null(redirectToActionResult.ControllerName); Assert.AreEqual("Index", redirectToActionResult.ActionName); } using (var context = new ApplicationDbContext(inMemoryDatabaseHelper.Options)) { Assert.That(context.Posts.Count(), Is.EqualTo(1)); } }
public async void Create_Errors() { PostsControllerMockFacade mock = new PostsControllerMockFacade(); var mockResponse = new Mock <CreateResponse <ApiPostsResponseModel> >(new FluentValidation.Results.ValidationResult()); var mockRecord = new ApiPostsResponseModel(); mockResponse.SetupGet(x => x.Success).Returns(false); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiPostsRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiPostsResponseModel> >(mockResponse.Object)); PostsController controller = new PostsController(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 ApiPostsRequestModel()); response.Should().BeOfType <ObjectResult>(); (response as ObjectResult).StatusCode.Should().Be((int)HttpStatusCode.UnprocessableEntity); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiPostsRequestModel>())); }
public async void Create_No_Errors() { PostsControllerMockFacade mock = new PostsControllerMockFacade(); var mockResponse = new CreateResponse <ApiPostsResponseModel>(new FluentValidation.Results.ValidationResult()); mockResponse.SetRecord(new ApiPostsResponseModel()); mock.ServiceMock.Setup(x => x.Create(It.IsAny <ApiPostsRequestModel>())).Returns(Task.FromResult <CreateResponse <ApiPostsResponseModel> >(mockResponse)); PostsController controller = new PostsController(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 ApiPostsRequestModel()); response.Should().BeOfType <CreatedResult>(); (response as CreatedResult).StatusCode.Should().Be((int)HttpStatusCode.Created); var createResponse = (response as CreatedResult).Value as CreateResponse <ApiPostsResponseModel>; createResponse.Record.Should().NotBeNull(); mock.ServiceMock.Verify(x => x.Create(It.IsAny <ApiPostsRequestModel>())); }
public void Create_ShouldReturnInstanceOfViewResult_WhenPostServiceReturnValidPost() { // Arrange var postsServiceMock = new Mock <IDataService <Post> >(); var categoriesServiceMock = new Mock <IDataService <PostCategory> >(); var usersServiceMock = new Mock <IDataService <ApplicationUser> >(); var commentsServiceMock = new Mock <IDataService <Comment> >(); var mockedMapper = new Mock <IMapper>(); var controller = new PostsController( postsServiceMock.Object, categoriesServiceMock.Object, usersServiceMock.Object, commentsServiceMock.Object, mockedMapper.Object); // Act var result = controller.Create(); // Assert Assert.IsInstanceOf <ViewResult>(result); }
public async Task CreatePostShouldWork() { // Arrange var mockCitiesService = new Mock <ICitiesService>(); var mockMakesService = new Mock <IMakesService>(); var mockCategoriesService = new Mock <ICategoriesService>(); var mockModelsService = new Mock <IModelsService>(); var mockVehicleCategoriesService = new Mock <IVehicleCategoriesService>(); var mockColorsService = new Mock <IColorService>(); var mockCloudinaryService = new Mock <ICloudinaryService>(); var mockCommentsService = new Mock <ICommentsService>(); var mockPostsService = new Mock <IPostsService>(); var mockUserManager = this.GetMockUserManager(); mockCategoriesService .Setup(mc => mc.GetCategoryByName("Cars and jeeps")) .Returns(new Category() { Id = 1, Name = "Cars and jeeps" }); mockCitiesService .Setup(mc => mc.GetCityByName("Sofia")) .Returns(new City() { Id = 1, Name = "Sofia" }); mockColorsService .Setup(mc => mc.GetColorByName("Blue")) .Returns(new Color() { Id = 1, Name = "Blue" }); mockMakesService .Setup(mc => mc.GetMakeById(11)) .Returns(new Make() { Id = 11, Name = "BMW" }); mockModelsService .Setup(mc => mc.GetModelByName("M5")) .Returns(new Model() { Id = 1, Name = "M5" }); mockVehicleCategoriesService .Setup(mc => mc.GetVehicleCategoryByName("Sedan")) .Returns(new VehicleCategory() { Id = 1, Name = "Sedan" }); var files = new List <IFormFile>(); using (var stream = File.OpenRead("../../../Files/cat.jpg")) { FormFile file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) { Headers = new HeaderDictionary(), ContentType = "image/jpeg", }; files.Add(file); } mockCloudinaryService .Setup(mc => mc.UploadAsync(files)) .Returns(Task.FromResult(this.GetAll <Image>())); var claims = new List <Claim>() { new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "5247d66a-84ff-4987-abb5-53b1c2e747c2"), new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "*****@*****.**"), new Claim("AspNet.Identity.SecurityStamp", "E7B2QZV5M4OIRM3ZFIVXFVGR3YULFGO7"), new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", "Admin"), new Claim("amr", "pwd"), }; var claimsIdentity = new ClaimsIdentity(claims); var principal = new ClaimsPrincipal(claimsIdentity); Thread.CurrentPrincipal = principal; mockUserManager.Setup(mu => mu.GetUserAsync(principal)) .Returns(Task.FromResult(new ApplicationUser() { UserName = "******", Image = new Image() { Url = "testUrl" } })); // Act var controller = new PostsController( mockPostsService.Object, mockCitiesService.Object, mockCategoriesService.Object, mockMakesService.Object, mockModelsService.Object, mockVehicleCategoriesService.Object, mockColorsService.Object, mockUserManager.Object, mockCloudinaryService.Object, mockCommentsService.Object); // Assert var inputModel = new CreatePageInputModel() { PostCategory = "Cars and jeeps", Town = "Sofia", Color = "Blue", Make = 11, Model = "M5", VehicleCategory = "Sedan", Condition = "Used", Currency = "LV", EngineType = "Disel", TransmissionType = "Automatic", Eurostandard = 5, Description = "some random description", Horsepower = 155, Email = "*****@*****.**", Year = 2020, Mileage = 152456, Modification = "F10", Price = 52000, PhoneNumber = "0897132123", Files = files, }; // Act var result = await controller.Create(inputModel); // Assert var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Posts", redirectToActionResult.ControllerName); Assert.Equal("Details", redirectToActionResult.ActionName); }
public void CreateGetShouldWork() { // Arrange var mockCitiesService = new Mock <ICitiesService>(); var mockMakesService = new Mock <IMakesService>(); var mockCategoriesService = new Mock <ICategoriesService>(); var mockModelsService = new Mock <IModelsService>(); var mockVehicleCategoriesService = new Mock <IVehicleCategoriesService>(); var mockColorsService = new Mock <IColorService>(); var mockCloudinaryService = new Mock <ICloudinaryService>(); var mockCommentsService = new Mock <ICommentsService>(); var mockPostsService = new Mock <IPostsService>(); var mockUserManager = this.GetMockUserManager(); mockCategoriesService .Setup(mc => mc.GetAll <Category>()) .Returns(this.GetAll <Category>()); mockCitiesService .Setup(mc => mc.GetAll <City>()) .Returns(this.GetAll <City>()); mockColorsService .Setup(mc => mc.GetAll <Color>()) .Returns(this.GetAll <Color>()); mockMakesService .Setup(mc => mc.GetAll <Make>()) .Returns(this.GetAll <Make>()); mockVehicleCategoriesService .Setup(mc => mc.GetAll <VehicleCategory>()) .Returns(this.GetAll <VehicleCategory>()); var claims = new List <Claim>() { new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "5247d66a-84ff-4987-abb5-53b1c2e747c2"), new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "*****@*****.**"), new Claim("AspNet.Identity.SecurityStamp", "E7B2QZV5M4OIRM3ZFIVXFVGR3YULFGO7"), new Claim("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", "Admin"), new Claim("amr", "pwd"), }; var claimsIdentity = new ClaimsIdentity(claims); var principal = new ClaimsPrincipal(claimsIdentity); Thread.CurrentPrincipal = principal; // Act var controller = new PostsController( mockPostsService.Object, mockCitiesService.Object, mockCategoriesService.Object, mockMakesService.Object, mockModelsService.Object, mockVehicleCategoriesService.Object, mockColorsService.Object, mockUserManager.Object, mockCloudinaryService.Object, mockCommentsService.Object); // Assert var result = controller.Create(); var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsAssignableFrom <CreatePageViewModel>(viewResult.ViewData.Model); Assert.Equal(3, model.Categories.Count()); Assert.Equal(3, model.Cities.Count()); Assert.Equal(3, model.Colors.Count()); Assert.Equal(3, model.Makes.Count()); Assert.Equal(3, model.VehicleCategories.Count()); }