public async Task InvalidDelete() { // Arrange Moq.Mock <IBloggingRepository> mock = MockBloggingRepository(); IBloggingRepository repository = mock.Object; var logger = LoggerFactory.CreateLogger <BlogsController>(); //1st add one object await repository.AddAsync(new Blog { BlogId = 99999, Post = null, Url = "http://www.99999.com", }); await repository.SaveAsync(); using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider)) { var model = new DecoratedBlog { BlogId = 99999, Post = null, Url = "bad_url_but_no_matter", ProtectedID = "giberish" }; // Act var result = await controller.BlogAction(model, true, BlogActionEnum.Delete); // Assert Assert.IsType <NotFoundResult>(result); Assert.Equal(404, ((NotFoundResult)result).StatusCode); } }
public async Task <ActionResult> Create(Blog blog) { var logger_tsk = Task.Run(() => { _logger.LogInformation(1, $"id = {blog.BlogId} url = {(blog.Url ?? "<null>")}"); }); if (ModelState.IsValid) { var appRootPath = _configuration.AppRootPath(); await _repo.AddAsync(blog); await _repo.SaveAsync(); var route = appRootPath + ASPX; return(Redirect(route)); } return(View(blog)); }
public async Task InvalidEdit() { // Arrange Moq.Mock <IBloggingRepository> mock = MockBloggingRepository(); IBloggingRepository repository = mock.Object; var logger = LoggerFactory.CreateLogger <BlogsController>(); //1st add one object await repository.AddAsync(new Blog { BlogId = -99999, Post = null, Url = "http://www.999999.com", }); await repository.SaveAsync(); using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider)) { var model = new DecoratedBlog { BlogId = -99999, Post = null, Url = "http://www.minus999999.com", ProtectedID = "giberish" }; // Act ((Controller)controller).ModelState.AddModelError(nameof(Blog.Url), "bad id"); var result = await controller.BlogAction(model, true, BlogActionEnum.Edit); // Assert Assert.IsType <JsonResult>(result); Assert.IsType <string>(((JsonResult)result).Value); Assert.Equal("error", ((JsonResult)result).Value.ToString()); } }
public async Task Edit() { // Arrange Moq.Mock <IBloggingRepository> mock = MockBloggingRepository(); IBloggingRepository repository = mock.Object; var logger = LoggerFactory.CreateLogger <BlogsController>(); //1st add one object await repository.AddAsync(new Blog { BlogId = 2, Post = null, Url = "http://www.internet.com", }); await repository.SaveAsync(); using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider)) { var model = new DecoratedBlog { BlogId = 2, Post = null, Url = "http://www.changed-internet.com", ProtectedID = "giberish" }; // Act var result = await controller.BlogAction(model, true, BlogActionEnum.Edit); // Assert Assert.IsType <JsonResult>(result); Assert.IsType <Blog>(((JsonResult)result).Value); Assert.Equal(2, ((Blog)((JsonResult)result).Value).BlogId); Assert.Contains("changed", ((Blog)((JsonResult)result).Value).Url); } }
public async Task Delete() { // Arrange Moq.Mock <IBloggingRepository> mock = MockBloggingRepository(); IBloggingRepository repository = mock.Object; var logger = LoggerFactory.CreateLogger <BlogsController>(); //1st add one object await repository.AddAsync(new Blog { BlogId = 2, Post = null, Url = "http://www.internet.com", }); await repository.SaveAsync(); using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider)) { var model = new DecoratedBlog { BlogId = 2, Post = null, Url = "bad_url_but_no_matter", ProtectedID = "giberish" }; // Act var result = await controller.ItemAction(model, true, BlogActionEnum.Delete); // Assert Assert.IsType <JsonResult>(result); Assert.IsType <string>(((JsonResult)result).Value); Assert.Equal("deleted", ((JsonResult)result).Value.ToString()); } }