Example #1
0
        public async Task InvalidCreate()
        {
            // Arrange
            Moq.Mock <IBloggingRepository> mock       = MockBloggingRepository();
            IBloggingRepository            repository = mock.Object;
            var logger = LoggerFactory.CreateLogger <BlogsController>();

            using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider))
            {
                // Act
                ((Controller)controller).ModelState.AddModelError(nameof(Blog.Url), "bad url");

                var model = new Blog
                {
                    BlogId = 2,
                    Post   = null,
                    Url    = "bad_url_is_bad"
                };
                var result = await controller.Create(model);

                // Assert
                Assert.NotNull(result);

                Assert.IsNotType <RedirectResult>(result);
                Assert.IsType <ViewResult>(result);
                Assert.NotNull(((ViewResult)result).Model);
                Assert.Same(model, ((ViewResult)result).Model);
            }
        }
Example #2
0
        public async Task Create()
        {
            // Arrange
            Moq.Mock <IBloggingRepository> mock       = MockBloggingRepository();
            IBloggingRepository            repository = mock.Object;
            var logger = LoggerFactory.CreateLogger <BlogsController>();

            using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider))
            {
                // Act
                var result = controller.Create();

                // Assert
                Assert.NotNull(result);

                Assert.IsType <ViewResult>(result);
                Assert.Null(((ViewResult)result).Model);
            }

            using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider))
            {
                // Act
                var result = await controller.Create(new Blog
                {
                    BlogId = 2,
                    Post   = null,
                    Url    = "http://www.internet.com"
                });

                // Assert
                Assert.NotNull(result);

                Assert.IsType <RedirectResult>(result);
                Assert.Equal($"{Configuration["AppRootPath"]}{BlogsController.ASPX}", ((RedirectResult)result).Url);
            }
        }