public void CallCreateStoryMethod_WhenParamsAreValid() { // Arrange var storyServiceMock = new Mock <IStoryService>(); var mappingServiceMock = new Mock <IMappingService>(); var userServiceMock = new Mock <IUserService>(); var utilsMock = new Mock <IUtilitiesService>(); var controller = new StoriesController(storyServiceMock.Object, mappingServiceMock.Object, userServiceMock.Object, utilsMock.Object); var controllerContext = new Mock <ControllerContext>(); var user = new Mock <IPrincipal>(); user.Setup(p => p.IsInRole("admin")).Returns(true); user.SetupGet(x => x.Identity.Name).Returns("username"); controllerContext.SetupGet(x => x.HttpContext.User).Returns(user.Object); controller.ControllerContext = controllerContext.Object; storyServiceMock.Setup(x => x.CreateStory(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())); var model = new CreateEditStoryViewModel(); // Act controller.CreateStory(model); // Assert storyServiceMock.Verify(x => x.CreateStory(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()), Times.Once); }
public void RediectToIndexAction_WhenParamsAreValid() { // Arrange var storyServiceMock = new Mock <IStoryService>(); var mappingServiceMock = new Mock <IMappingService>(); var userServiceMock = new Mock <IUserService>(); var utilsMock = new Mock <IUtilitiesService>(); var controller = new StoriesController(storyServiceMock.Object, mappingServiceMock.Object, userServiceMock.Object, utilsMock.Object); var controllerContext = new Mock <ControllerContext>(); var user = new Mock <IPrincipal>(); user.Setup(p => p.IsInRole("admin")).Returns(true); user.SetupGet(x => x.Identity.Name).Returns("username"); controllerContext.SetupGet(x => x.HttpContext.User).Returns(user.Object); controller.ControllerContext = controllerContext.Object; storyServiceMock.Setup(x => x.CreateStory(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())); var model = new CreateEditStoryViewModel(); // Act & Assert controller.WithCallTo(x => x.CreateStory(model)) .ShouldRedirectTo(x => x.Index(null, null)); }
public ActionResult CreateStory(CreateEditStoryViewModel model) { if (!this.ModelState.IsValid) { return(this.View(model)); } var userId = this.User.Identity.GetUserId(); this.storyService.CreateStory(model.Title, model.Content, model.RelatedDestination, model.ImageUrl, userId); return(this.RedirectToAction("Index")); }
public void ReturnDefaultViewWithRespectiveViewModel_WhenModelStateIsInvalid() { // Arrange var storyServiceMock = new Mock <IStoryService>(); var mappingServiceMock = new Mock <IMappingService>(); var userServiceMock = new Mock <IUserService>(); var utilsMock = new Mock <IUtilitiesService>(); var controller = new StoriesController(storyServiceMock.Object, mappingServiceMock.Object, userServiceMock.Object, utilsMock.Object); controller.ViewData.ModelState.AddModelError("asdasd", "asdas"); var model = new CreateEditStoryViewModel(); // Act & Assert controller.WithCallTo(x => x.EditStory(model)) .ShouldRenderDefaultView() .WithModel <CreateEditStoryViewModel>(x => x == model); }
public void RenderDetailsView_OnSuccessfulEdit() { // Arrange var storyServiceMock = new Mock <IStoryService>(); var mappingServiceMock = new Mock <IMappingService>(); var userServiceMock = new Mock <IUserService>(); var utilsMock = new Mock <IUtilitiesService>(); var controller = new StoriesController(storyServiceMock.Object, mappingServiceMock.Object, userServiceMock.Object, utilsMock.Object); var story = new Story(); storyServiceMock.Setup(x => x.GetById(It.IsAny <Guid>())).Returns(story); var viewModel = new StoryDetailsViewModel(); mappingServiceMock.Setup(x => x.Map <StoryDetailsViewModel>(story)).Returns(viewModel); var controllerContext = new Mock <ControllerContext>(); var user = new Mock <IPrincipal>(); user.Setup(p => p.IsInRole("admin")).Returns(true); user.SetupGet(x => x.Identity.Name).Returns("username"); controllerContext.SetupGet(x => x.HttpContext.User).Returns(user.Object); controller.ControllerContext = controllerContext.Object; var userModel = new User(); userServiceMock.Setup(x => x.GetById(It.IsAny <string>())).Returns(userModel); storyServiceMock.Setup(x => x.IsStoryLiked(It.IsAny <Guid>(), It.IsAny <string>())).Returns(true); var model = new CreateEditStoryViewModel(); // Act & Assert controller.WithCallTo(x => x.EditStory(model)) .ShouldRenderView("Details") .WithModel <StoryDetailsViewModel>(x => x == viewModel); }
public ActionResult EditStory(CreateEditStoryViewModel model) { if (!this.ModelState.IsValid) { return(this.View(model)); } this.storyService.EditStory(model.Id, model.Title, model.Content, model.RelatedDestination, model.ImageUrl); var story = this.storyService.GetById(model.Id); var viewModel = this.mappingService.Map <StoryDetailsViewModel>(story); var userId = this.User.Identity.GetUserId(); var currentUser = this.userService.GetById(userId); if (currentUser != null) { viewModel.CurrentUser = currentUser; viewModel.IsStoryLiked = this.storyService.IsStoryLiked(story.Id, currentUser.Id); } return(this.View("Details", viewModel)); }
public void RenderDefaultView_WhenParamsAreValid() { // Arrange var storyServiceMock = new Mock <IStoryService>(); var mappingServiceMock = new Mock <IMappingService>(); var userServiceMock = new Mock <IUserService>(); var utilsMock = new Mock <IUtilitiesService>(); var controller = new StoriesController(storyServiceMock.Object, mappingServiceMock.Object, userServiceMock.Object, utilsMock.Object); var story = new Story(); storyServiceMock.Setup(x => x.GetById(It.IsAny <Guid>())).Returns(story); var model = new CreateEditStoryViewModel(); mappingServiceMock.Setup(x => x.Map <CreateEditStoryViewModel>(story)).Returns(model); // Act & Assert controller.WithCallTo(x => x.EditStory(Guid.NewGuid())) .ShouldRenderDefaultView() .WithModel <CreateEditStoryViewModel>(x => x == model); }