2012-09-11 2 views
1

NUnit과 함께 Sharp Architechture 및 Rhino Mock을 사용하고 있습니다.RhinoMocks 서비스 테스트

가 난 다음 내 서비스에이 방법이이

public class TestService : ITestService { 
    public TestService(ITestQueries testQueries, IRepository<Test> testRepository, 
         IApplicationCachedListService applicationCachedListService) { 
     Check.Require(testQueries != null, "testQueries may not be null"); 
     Check.Require(applicationCachedListService != null, "applicationCachedListService may not be null"); 
     _testQueries = testQueries; 
     _testRepository = testRepository; 
     _applicationCachedListService = applicationCachedListService; 
    } 

과 같은 테스트 서비스를

public string Create(TestFormViewModel viewModel, ViewDataDictionary viewData, TempDataDictionary tempData) { 
     if (!viewData.ModelState.IsValid) { 
      tempData.SafeAdd(viewModel); 
      return "Create"; 
     } 

     try { 
      var test = new Test(); 
      UpdateFromViewModel(test, viewModel); 
      _testRepository.SaveOrUpdate(test); 
      tempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] 
       = string.Format("Successfully created product '{0}'", test.TestName); 
     } 
     catch (Exception ex) { 
      _testRepository.DbContext.RollbackTransaction(); 
      tempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] 
       = string.Format("An error occurred creating the product: {0}", ex.Message); 
      return "Create"; 
     } 

     return "Index"; 


    } 

}

I는 다음과 같습니다 컨트롤러를 가지고

:

[ValidateAntiForgeryToken] 
    [Transaction] 
    [AcceptVerbs(HttpVerbs.Post)] 
    [ModelStateToTempData] 
    public ActionResult Create(TestFormViewModel viewModel) { 
     return RedirectToAction(_testService.Create(viewModel, ViewData, TempData)); 
    } 

나는 언제 viewData.ModelState.IsValid를 볼 수있는 간단한 테스트를 작성하고 싶습니다. "Create"를 리턴합니다.

저는 지금까지 이것을 가지고 있습니다 만, 실제로 컨트롤러를 테스트하지 않기 때문에 혼란 스럽습니다. 반환 할 때 말하고있는 것을 그냥하고있는 것입니다.

[Test] 
    public void CreateResult_RedirectsToActionCreate_WhenModelStateIsInvalid(){ 
     // Arrange 
     var viewModel = new TestFormViewModel(); 
     _controller.ViewData.ModelState.Clear(); 
     _controller.ModelState.AddModelError("Name", "Please enter a name"); 


     _testService.Stub(a => a.Create(viewModel, new ViewDataDictionary(), new TempDataDictionary())).IgnoreArguments().Return("Create"); 

     // Act 
     var result = _controller.Create(viewModel); 

     // Assert    
     result.AssertActionRedirect().ToAction("Create"); //this is really not testing the controller??. 



    } 

도움을 주시면 감사하겠습니다.

답변

0

단위 테스트를 쓰지 않으려 고합니다. 그것은 통합 테스트와 더 비슷합니다. 단위 테스트 이데올로기에 이어 두 개의 단위가 있습니다 : ServiceController. 아이디어는 각 장치를 개별적으로 테스트하고 테스트를 간단하게 유지해야한다는 것입니다. 이 첫 번째에 따르면 TestService에 대한 테스트를 작성해야합니다. 그 후, 그것을 커버 할 때 TestService에 대해 스텁/모의를 사용하여 Controller에 대한 테스트를 작성하십시오. 따라서 Controller에 대한 귀하의 테스트는 올바르게 보입니다. Service.Create 메서드의 결과에 따라 리디렉션이 발생했는지 테스트합니다. 컨트롤러 컨텍스트없이 TestService에 대한 테스트를 추가해야하며 좋은 적용 범위가 있습니다. 이 단위를 함께 테스트하려면 mock을 사용하지 않아야하며 통합 테스트와 더 비슷합니다. 또한 모듈 간의 통합을 다루기 위해 전체 애플리케이션 테스트를 위해 WatiN 또는 Selenium과 같은 도구를 사용하여 웹 기반 테스트를 작성할 수 있습니다. 그러나 어떤 경우 든 개별 부품에 대한 단위 테스트를 작성하는 것이 좋습니다.

+0

감사합니다. – TheCodeFool