私のアプリケーションにはかなり基本的なHomeControllerがあり、そのためのテストクラスを作成しました。テストクラスでコードカバレッジ分析を実行した後、デフォルトのコンストラクターのテストがないことに気付きました。
これは私のHomeControllerです
public class HomeController : BaseController
{
private INewsRepository _newsRepository;
private INewsletterRepository _newsletterRepository;
private string _currentLanguage;
public HomeController()
{
_newsRepository = NewsRepository.Current;
_newsletterRepository = NewsletterRepository.Current;
_currentLanguage = ApplicationConfig.Current.CurrentLanguage;
}
public HomeController(INewsRepository newsRepository, INewsletterRepository newsletterRepository, string currentLanguage)
{
_newsRepository = newsRepository;
_newsletterRepository = newsletterRepository;
_currentLanguage = currentLanguage;
}
public ActionResult Index()
{
return View();
}
public ActionResult LatestNews()
{
return View(_newsRepository.ListAll().Where(n => n.LanguageKey.ToLower() == _currentLanguage.ToLower()).OrderByDescending(n => n.Date).Take(10));
}
public ActionResult LatestNewsletters()
{
return View(_newsletterRepository.ListAll().Where(n => n.LanguageKey.ToLower() == _currentLanguage.ToLower()).OrderByDescending(n => n.Date).Take(10));
}
}
そして、これは私のテストクラスです:
[TestClass]
public class HomeControllerTest
{
private INewsletterRepository _mockNewsletterRepostiory = null;
private INewsRepository _mockNewsRepostiory = null;
private List<News> _fakeNewsList = new List<News> {
new News{Id = 0, Title = "some title", Date = new DateTime(1989, 2, 19), LanguageKey = "fa", Description = "some description"},
new News{Id = 1, Title = "some title", Date = new DateTime(1989, 2, 20), LanguageKey = "fa", Description = "some description"},
new News{Id = 2, Title = "some title", Date = new DateTime(1989, 2, 21), LanguageKey = "fa", Description = "some description"},
new News{Id = 3, Title = "some title", Date = new DateTime(1989, 2, 22), LanguageKey = "fa", Description = "some description"}
};
private List<Newsletter> _fakeNewsletterList = new List<Newsletter>
{
new Newsletter{ Id = 0, Description = "some description", UrlKey = "first-newsletter", Title = "some title", SendDate = null, NewsletterContents = null, LanguageKey = "fa", Date = new DateTime(1989, 2, 19) },
new Newsletter{ Id = 1, Description = "some description", UrlKey = "first-newsletter", Title = "some title", SendDate = null, NewsletterContents = null, LanguageKey = "fa", Date = new DateTime(1989, 2, 20) },
new Newsletter{ Id = 2, Description = "some description", UrlKey = "first-newsletter", Title = "some title", SendDate = null, NewsletterContents = null, LanguageKey = "fa", Date = new DateTime(1989, 2, 21) },
new Newsletter{ Id = 3, Description = "some description", UrlKey = "first-newsletter", Title = "some title", SendDate = null, NewsletterContents = null, LanguageKey = "fa", Date = new DateTime(1989, 2, 22) }
};
[TestInitialize]
public void Setup()
{
// Mock News Repository
var mockNewsRepository = MockRepository.GenerateStub<INewsRepository>();
mockNewsRepository.Stub(m => m.ListAll()).Return(_fakeNewsList.AsQueryable());
// Mock Newsletter Repository
var mockNewsletterRopository = MockRepository.GenerateStub<INewsletterRepository>();
mockNewsletterRopository.Stub(m => m.ListAll()).Return(_fakeNewsletterList.AsQueryable());
_mockNewsletterRepostiory = mockNewsletterRopository;
_mockNewsRepostiory = mockNewsRepository;
}
[TestMethod]
public void IndexReturnsView()
{
// Arrange
HomeController homeController = new HomeController(_mockNewsRepostiory, _mockNewsletterRepostiory, "fa");
// Act
ViewResult result = homeController.Index() as ViewResult;
// Assert
Assert.AreEqual("", result.ViewName);
}
[TestMethod]
public void LatestNewsReturnsCorrectObject()
{
// Arrange
HomeController homeController = new HomeController(_mockNewsRepostiory, _mockNewsletterRepostiory, "fa");
// Act
ViewResult result = homeController.LatestNews() as ViewResult;
// Assert
Assert.IsNotNull(result.ViewData.Model, "Result model is not null.");
Assert.IsTrue(_fakeNewsList.OrderByDescending(n => n.Date).SequenceEqual(result.ViewData.Model as IQueryable<News>), "Model is correct");
}
[TestMethod]
public void LatestNewslettersReturnsCorrectObject()
{
// Arrange
HomeController homeController = new HomeController(_mockNewsRepostiory, _mockNewsletterRepostiory, "fa");
// Act
ViewResult result = homeController.LatestNewsletters() as ViewResult;
// Assert
Assert.IsNotNull(result.ViewData.Model, "Result model is not null.");
Assert.IsTrue(_fakeNewsletterList.OrderByDescending(n => n.Date).SequenceEqual(result.ViewData.Model as IQueryable<Newsletter>), "Model is correct");
}
}
まず第一に、私は本当にこのコンストラクターをテストする必要がありますか?第二に、_newsRepositoryと他の人を公開して読み取り専用にし、コントローラーを構築した後にそれらのタイプをチェックするためのテストメソッドを書くのは良い習慣ですか?