public void ReturnPartialViewWithRespectiveModel_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.ToggleLike(It.IsAny <Guid>(), It.IsAny <string>())); var story = new Story(); storyServiceMock.Setup(x => x.GetById(It.IsAny <Guid>())).Returns(story); var model = new StoryDetailsViewModel(); mappingServiceMock.Setup(x => x.Map <StoryDetailsViewModel>(story)).Returns(model); // Act & Assert controller.WithCallTo(x => x.LikeStory(Guid.NewGuid())) .ShouldRenderPartialView("_UnlikeButtonStoryPartial") .WithModel <StoryDetailsViewModel>(x => x == model); }
public void Returns_data_From_Service_verbatim() { //Arrange var stories = new [] { new Story(), new Story(), new Story() }; var service = new Mock <IStoryService>(); service .Setup(fake => fake.GetAllAsync()) .ReturnsAsync(stories); var controller = new StoriesController(service.Object); //Act var result = controller.Get().Result; //Assert service.Verify(mock => mock.GetAllAsync(), Times.Once); Assert.AreEqual(stories.Count(), result.Count()); Assert.AreEqual(stories.First(), result.First()); }
public void UpdateGet_ShouldReturnRedirect_IfNotAuthorAndNotAdmin() { user.Stories = new List <Story>(); var service = new Mock <IStoryService>(); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Role, "User") })), }, }, TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; var result = controller.Update("testId"); var viewResult = Assert.IsAssignableFrom <RedirectResult>(result); viewResult.Url.ShouldBe("/Stories/Details/testId"); }
public void CallToggleLikeMethod_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.ToggleLike(It.IsAny <Guid>(), It.IsAny <string>())); var story = new Story(); storyServiceMock.Setup(x => x.GetById(It.IsAny <Guid>())).Returns(story); var model = new StoryDetailsViewModel(); mappingServiceMock.Setup(x => x.Map <StoryDetailsViewModel>(story)).Returns(model); // Act controller.LikeStory(Guid.NewGuid()); // Assert storyServiceMock.Verify(x => x.ToggleLike(It.IsAny <Guid>(), It.IsAny <string>()), Times.Once); }
public async Task Create_ShouldReturnRedirect_OnException() { var service = new Mock <IStoryService>(); service.Setup(s => s.CreateAsync("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", user)) .Throws(new Exception()); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; CreateStoryInputModel model = new CreateStoryInputModel { Title = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." }; var result = await controller.Create(model); var viewResult = Assert.IsAssignableFrom <RedirectResult>(result); viewResult.Url.ShouldBe("/Stories"); controller.TempData.ContainsKey("notification").ShouldBeTrue(); controller.TempData["notification"].ShouldNotBeNull(); controller.TempData["notification"].ShouldBeOfType <string[]>(); string[] arr = controller.TempData["notification"] as string[]; arr[0].ShouldBe("danger"); }
public void UpdateGet_ShouldReturnRedirect_IfNotAuthorButAdmin() { user.Stories = new List <Story>(); var service = new Mock <IStoryService>(); service.Setup(s => s.GetStoryByIdAsViewModel("testId")) .Returns(() => new StoryViewModel { Id = "testId" }); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Role, "Administrator") })), }, }, TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; var result = controller.Update("testId"); var viewResult = Assert.IsAssignableFrom <ViewResult>(result); viewResult.Model.ShouldBeOfType <StoryViewModel>(); viewResult.Model.ShouldNotBeNull(); }
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 async Task Create_ShouldReturnView_OnInValidModel() { var service = new Mock <IStoryService>(); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; controller.ModelState.AddModelError("k", "e"); CreateStoryInputModel model = new CreateStoryInputModel { Title = "" }; var result = await controller.Create(model); var viewResult = Assert.IsAssignableFrom <ViewResult>(result); viewResult.Model.ShouldBeOfType <CreateStoryInputModel>(); controller.TempData.ContainsKey("notification").ShouldBeTrue(); controller.TempData["notification"].ShouldNotBeNull(); controller.TempData["notification"].ShouldBeOfType <string[]>(); string[] arr = controller.TempData["notification"] as string[]; arr[0].ShouldBe("danger"); }
public async Task Delete_ShouldReturnRedirect_IfNotAuthorAndNotAdmin() { user.Stories = new List <Story>(); var service = new Mock <IStoryService>(); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Role, "User") })), }, }, TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; var result = await controller.Delete("testId"); result.ShouldNotBeNull(); var viewResult = Assert.IsAssignableFrom <RedirectResult>(result); viewResult.Url.ShouldBe("/Stories/Details/testId"); controller.TempData.ContainsKey("notification").ShouldBeTrue(); controller.TempData["notification"].ShouldNotBeNull(); controller.TempData["notification"].ShouldBeOfType <string[]>(); string[] arr = controller.TempData["notification"] as string[]; arr[0].ShouldBe("danger"); }
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 void SetUp() { _endPointMock = new Mock <ICachedStoriesEndPointDecorator>(); _endPointMock.Setup(e => e.GetBestStoriesAsync(CancellationToken.None)) .ReturnsAsync(() => StoriesTestHelper.GenerateStories(200).ToList()); _loggerMock = new Mock <ILogger <StoriesController> >(); _storiesController = new StoriesController(_endPointMock.Object, _loggerMock.Object); }
public void SetUp() { var viewMapperMock = new Mock <IViewEntityModelMapper <Story, ViewStoryModel> >(); editMapperMock = new Mock <IEditEntityModelMapper <Story, EditStoryModel> >(); repository = new RepositoryMock <Story>(); controller = new StoriesController(repository, viewMapperMock.Object, editMapperMock.Object); }
public void ReturnsNotFoundIfStoryNotExists() { Mock <IStoryService> storyService = new Mock <IStoryService>(); storyService.Setup(s => s.GetStory(1)).Returns <Story>(null); StoriesController storiesController = new StoriesController(storyService.Object, null); var result = storiesController.Get(1); Assert.IsInstanceOfType(result, typeof(NotFoundResult)); }
public void Get_Should_Return_A_Story() { var mockService = new Mock <IStoryService>(); mockService.Setup(ms => ms.GetStory()).Returns("This is my story"); var storiesController = new StoriesController(mockService.Object); var result = storiesController.Get(); Assert.NotEmpty(result); }
public void ReturnsAStoryById() { Mock <IStoryService> storyService = new Mock <IStoryService>(); storyService.Setup(s => s.GetStory(1)).Returns(new StoryBuilder().WithTitle("title 1")); StoriesController storiesController = new StoriesController(storyService.Object, null); var result = storiesController.Get(1) as OkNegotiatedContentResult <StoryResponse>; Assert.IsNotNull(result); Assert.AreEqual("title 1", result.Content.Title); }
public void Get_WhenCalled_ReturnsAllItems() { // Act var _service = new storyFakeHttpClient(); var cache = new MemoryCache(new MemoryCacheOptions()); var _controller = new StoriesController(_service, cache); var okResult = _controller.GetAsync().Result as OkObjectResult; // Assert var items = Assert.IsType <List <NewStory> >(okResult.Value); Assert.Equal(3, items.Count); }
public StoriesControllerTest(ITestOutputHelper testOutputHelper) { Claim claim = new Claim(ClaimTypes.Sid, "akash"); List <Claim> claims = new List <Claim> { claim }; this.testOutputHelper = testOutputHelper; httpContext = CreateHttpContext(claims); storiesService = new Mock <IStoriesService>(); storiesController = new StoriesController(storiesService.Object); }
public async Task UpdatePost_ShouldReturnView_OnInValidModel() { user.Stories = new List <Story> { new Story { Id = "testId", AuthorId = "testId" } }; var model = new StoryViewModel { Id = "testId", Author = user }; var service = new Mock <IStoryService>(); service.Setup(s => s.UpdateAsync(model)) .ReturnsAsync(() => true); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Role, "Administrator") })), }, }, TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; controller.ModelState.AddModelError("K", "E"); var result = await controller.Update(model); result.ShouldNotBeNull(); var viewResult = Assert.IsAssignableFrom <ViewResult>(result); viewResult.Model.ShouldBeOfType <StoryViewModel>(); controller.TempData.ContainsKey("notification").ShouldBeTrue(); controller.TempData["notification"].ShouldNotBeNull(); controller.TempData["notification"].ShouldBeOfType <string[]>(); string[] arr = controller.TempData["notification"] as string[]; arr[0].ShouldBe("danger"); }
public void ReturnDefaultView() { // 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); // Act & Assert controller.WithCallTo(x => x.CreateStory()) .ShouldRenderDefaultView(); }
public void ReturnCorrectInstance_WhenParamsAreValid() { // Arrange var storyServiceMock = new Mock <IStoryService>(); var mappingServiceMock = new Mock <IMappingService>(); var userServiceMock = new Mock <IUserService>(); var utilsMock = new Mock <IUtilitiesService>(); // Act var controller = new StoriesController(storyServiceMock.Object, mappingServiceMock.Object, userServiceMock.Object, utilsMock.Object); // Assert Assert.IsInstanceOf <StoriesController>(controller); }
public void RedirectToIndexAction_WhenPassedIdIsNull() { // 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); // Act & Assert controller.WithCallTo(x => x.EditStory((Guid?)null)) .ShouldRedirectTo(x => x.Index(null, null)); }
public void ReturnHttpNotFound_WhenNoSucStoryIsFound() { // 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); // Act & Assert controller.WithCallTo(x => x.EditStory(Guid.NewGuid())) .ShouldGiveHttpStatus(404); }
public void ReturnsNotFoundIfThereAreNotLastestStoriesWithSpecifications() { Mock <IStoryService> storyService = new Mock <IStoryService>(); storyService.Setup(s => s.GetLastestStories(It.IsAny <string>(), It.IsAny <Pagination>())).Returns <IEnumerable <Story> >(null); StoriesController storiesController = new StoriesController(storyService.Object, null); var result = storiesController.GetHome(new StoryFilterRequest() { }); Assert.IsInstanceOfType(result, typeof(NotFoundResult)); }
public async Task UpdatePost_ShouldReturnView_IfAllIsOkAndServiceTrue() { user.Stories = new List <Story> { new Story { Id = "testId", AuthorId = "testId" } }; var model = new StoryViewModel { Id = "testId", Author = user, Content = "Lorem ipsum dolor sit amet,consectetur adipiscing eli" }; var service = new Mock <IStoryService>(); service.Setup(s => s.UpdateAsync(model)) .ReturnsAsync(() => true); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Role, "Administrator") })), }, }, TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; var result = await controller.Update(model); result.ShouldNotBeNull(); var viewResult = Assert.IsAssignableFrom <RedirectResult>(result); viewResult.Url.ShouldBe("/Stories/Details/testId"); controller.TempData.ContainsKey("notification").ShouldBeTrue(); controller.TempData["notification"].ShouldNotBeNull(); controller.TempData["notification"].ShouldBeOfType <string[]>(); string[] arr = controller.TempData["notification"] as string[]; arr[0].ShouldBe("success"); }
public void Put_sends_data_straight_to_service() { //Arrange var service = new Mock <IStoryService>(); var controller = new StoriesController(service.Object); var id = 42; var story = new Story(); //Act controller.Put(id, story).Wait(); //Assert service.Verify(mock => mock.UpdateAsync(id, story), Times.Once); }
public void Index_ShouldReturnView() { var service = new Mock <IStoryService>(); service.Setup(s => s.GetAllStoriesAsViewModels()) .Returns(() => new List <StoryViewModel>()); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger); var result = controller.Index(); var viewResult = Assert.IsAssignableFrom <ViewResult>(result); viewResult.Model.ShouldBeOfType <List <StoryViewModel> >(); viewResult.Model.ShouldNotBeNull(); }
public void ThrowInvalidOperationException_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); var model = new StoryDetailsViewModel(); controller.ViewData.ModelState.AddModelError("asdasda", "asdasdas"); // Act & Assert Assert.Throws <InvalidOperationException>(() => controller.Comment(model, Guid.NewGuid())); }
public void CreateStory_Should_Return_InvalidModel_For_Wrong_File_Type() { var fileMock = new Mock <IFormFile>(); fileMock.Setup(x => x.ContentType).Returns("image/sss"); var story = new Mock <StoryInputModel>(); story.Object.StoryImage = fileMock.Object; story.SetupAllProperties(); var controller = new StoriesController(storyService.Object); var result = controller.CreateStory(story.Object).GetAwaiter().GetResult(); result.Should().BeOfType <ViewResult>().Which.ViewData.Values.Should().Contain(GlobalConstants.WrongFileType); }
public async Task Favorite_ShouldReturnTrue() { var service = new Mock <IStoryService>(); service.Setup(s => s.FavoriteAsync("id", user)) .ReturnsAsync(() => true); StoriesController controller = new StoriesController( service.Object, userManager.Object, logger) { TempData = new TempDataDictionary(httpContext.Object, tempDataProvider.Object) }; var result = await controller.Favorite("id"); result.ShouldBeTrue(); }
public void CallDeleteStoryMethod_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); storyServiceMock.Setup(x => x.DeleteStory(It.IsAny <Guid>())); // Act controller.DeleteStory(Guid.NewGuid()); // Assert storyServiceMock.Verify(x => x.DeleteStory(It.IsAny <Guid>()), Times.Once); }