public void Index_GetCorrect() { var mockHttpService = new Mock <IHttpService>(); var mockTodoService = new Mock <ITodoService>(); mockHttpService .Setup(_ => _.doGet("https://jsonplaceholder.typicode.com/todos")) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent("[{'id':1,'userId': 1, 'title': 'asdasd', 'completed': false}]") }); TodoService t = new TodoService(mockHttpService.Object); TodoController controller = new TodoController(t); var resultado = controller.Index().Result; // ViewBag NO tiene que tener los errores. Assert.IsNull(controller.ViewBag.Error); Assert.IsNotNull(resultado); // Verifica que el tipo sea ActionResult Assert.IsInstanceOfType(resultado, typeof(ActionResult)); }
public void Index_ReturnsViewResult_WithAListOfTodos_WhenSucceeded() { var result = controller.Index(); var viewResult = Assert.IsType <ViewResult>(result); Assert.IsAssignableFrom <List <Todo> >(viewResult.Model); }
public void Should_Display_A_List_Of_Todo_Items() { TodoController controller = new TodoController(); var viewResult = controller.Index() as ViewResult; Assert.AreEqual(GetOrganized.Models.Todo.ThingsToBeDone, viewResult.ViewData.Model); }
public void DeleteTest() { ITaskRepository repo = new ObjectTaskRepository(); TodoController controller = new TodoController(repo); controller.Delete(-1); repo.Delete(-1); ViewResult result = controller.Index() as ViewResult; Assert.AreEqual((result.Model as TodoIndexViewModel).Tasks.Count, repo.GetAll().Count); controller.Delete(2); repo.Delete(2); result = controller.Index() as ViewResult; Assert.AreEqual((result.Model as TodoIndexViewModel).Tasks.Count, repo.GetAll().Count); }
public void Foo() { //arrange var controller = new TodoController(); //act var todos = controller.Index(); //assert Assert.Single(todos); Assert.Equal("test 123", todos.FirstOrDefault()?.Name); }
public void Index_VerificarLlamadoDeMetodos() { var mockTodoService = new Mock <ITodoService>(); mockTodoService.Setup(_ => _.GetAll()).ReturnsAsync(new List <Todo>()); TodoController controller = new TodoController(mockTodoService.Object); var resultado = controller.Index().Result; mockTodoService.Verify(_ => _.GetAll(), Times.Once); }
public async Task Index_ShouldReturn_ChallengeResult_IfUserIsUnauthorize() { // Arrange // Make the mockUserManager always return null for GetUserAsync mockUserManager .Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>())) .Returns(Task.FromResult <IdentityUser>(null)); var controller = new TodoController(mockTodoItemService.Object, mockCategoryService.Object, mockUserManager.Object); // Act var result = await controller.Index(); // Assert Assert.IsType <ChallengeResult>(result); }
public async Task Index_ShouldReturn_ViewResult_TodoViewModel() { // Arrange // Make the mockUserManager always return fake user for GetUserAsync var fakeUser = new IdentityUser(); mockUserManager .Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>())) .Returns(Task.FromResult <IdentityUser>(fakeUser)); var controller = new TodoController(mockTodoItemService.Object, mockCategoryService.Object, mockUserManager.Object); // Act var result = await controller.Index(); // Assert var viewResult = Assert.IsType <ViewResult>(result); Assert.IsAssignableFrom <TodoViewModel>(viewResult.ViewData.Model); }
public void Should_Set_Loggin_User_To_View_Data() { var nameUser = "******"; var rmContext = new Mock <HttpContext>(); var user = new GenericPrincipal(new GenericIdentity(nameUser), null); rmContext.Setup(x => x.User).Returns(user); var todoController = new TodoController(null); var controllerContext = new ControllerContext(); controllerContext.HttpContext = rmContext.Object; todoController.ControllerContext = controllerContext; //todoController.HttpContext.User = new GenericPrincipal(new GenericIdentity("Jonathan"), null); var result = (ViewResult)todoController.Index(); Assert.IsType <RedirectToActionResult>(result); Assert.Equal(nameUser, result.ViewData["UserName"]); }
public void Index_ManejoDeError() { var mockHttpService = new Mock <IHttpService>(); var mockTodoService = new Mock <ITodoService>(); mockHttpService .Setup(_ => _.doGet("https://jsonplaceholder.typicode.com/todos")) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); TodoService t = new TodoService(mockHttpService.Object); TodoController controller = new TodoController(t); var resultado = controller.Index().Result; // ViewBag tiene que tener los errores. Assert.IsNotNull(controller.ViewBag.Error); // ViewBag tiene que tener los errores. Assert.IsNotNull(resultado); // Verifica que el tipo sea ActionResult Assert.IsInstanceOfType(resultado, typeof(ActionResult)); }