Created
September 3, 2014 13:10
-
-
Save cpoDesign/a6e50aeee3f3590353e9 to your computer and use it in GitHub Desktop.
Unit testing RedirectToAction in controller MVC5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlogController : Controller | |
{ | |
private IArticleRepository ArticleData { get; set; } | |
private IUserRepository UserRepository { get; set; } | |
public BlogController(IArticleRepository articleRepository, IUserRepository userRepository) | |
{ | |
if(articleRepository == null) throw new ArgumentNullException("articleRepository"); | |
if (userRepository == null) throw new ArgumentNullException("userRepository"); | |
ArticleData = articleRepository; | |
UserRepository = userRepository; | |
} | |
public ActionResult Index() | |
{ | |
return View(); | |
} | |
public ActionResult Detail(int year, int month, string title) | |
{ | |
var article = ArticleData.GetArticle(year, month, title); | |
if (article == null) return RedirectToAction("Index", "Blog"); | |
var model = new TestModel(); | |
return View(model); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using blog.Controllers; | |
using Blog.Core.Entities; | |
using Blog.Core.Intefaces; | |
using blog.Models.Blog; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System.Web.Mvc; | |
using Moq; | |
namespace blog.Tests.Controllers | |
{ | |
[TestClass] | |
public class BlogControllerTest | |
{ | |
private Mock<IArticleRepository> _articleRepository; | |
private Mock<IUserRepository> _userRepository; | |
[TestInitialize] | |
public void Init() | |
{ | |
_articleRepository = new Mock<IArticleRepository>(); | |
_userRepository = new Mock<IUserRepository>(); | |
} | |
[TestMethod] | |
public void BlogControllerIndex() | |
{ | |
// Assign | |
var list = new List<Article>(); | |
// Mock | |
_articleRepository.Setup(x => x.GetArticlesForYearMonth(It.IsAny<int>(), It.IsAny<int>())) | |
.Returns(list); | |
// Arrange | |
var controller = new BlogController(_articleRepository.Object, _userRepository.Object); | |
// Act | |
var result = controller.Index() as ViewResult; | |
var model = result.Model as BlogModel; | |
// Assert | |
Assert.AreEqual(list.Count(), model.Articles.Count()); | |
} | |
[TestMethod] | |
public void BlogControllerDetailWhenNullRedirectsToIndex() | |
{ | |
// Assign | |
Article article = null; | |
// Mock | |
_articleRepository.Setup(x => x.GetArticle(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>)) | |
.Returns(article); | |
// Arrange | |
var controller = new BlogController(_articleRepository.Object, _userRepository.Object); | |
// Act | |
var result = (RedirectToRouteResult)controller.Detail(1,1,"test"); | |
// Assert | |
Assert.AreEqual("Index", result.RouteValues["action"]); | |
Assert.AreEqual("Blog", result.RouteValues["controller"]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment