1

NUnitでSharpArchitechtureと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";


    }

}

次に、次のようなコントローラーがあります。

[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??.



    }

どんな助けでも大歓迎です。

4

1 に答える 1

0

ユニットテストではなく、書き込もうとしているようです。統合テストのようなものです。ユニットテストのイデオロギーに続いて、2つのユニットがあります:ServiceController。アイデアは、各ユニットを個別にテストし、テストを単純に保つ必要があるということです。これによると、まず最初に、のテストを作成する必要がありますTestService。その後、それをカバーするときに、Controllerスタブ/モックを使用するためのテストを記述しますTestService。したがって、のテストControllerは正しく見えます。メソッドの結果に従ってリダイレクトが行われることをテストしService.Createます。テストを追加する必要がありますTestServiceコントローラのコンテキストがなくても、十分なカバレッジが得られます。このユニットを一緒にテストする場合は、モックを使用しないでください。統合テストのようになります。さらに、モジュール間の統合をカバーするために、アプリケーション全体をテストするためのWatiNやSeleniumなどのツールを使用してWebベースのテストを作成できます。ただし、いずれの場合も、個別のパーツの単体テストを作成することをお勧めします。

于 2012-09-11T20:26:14.883 に答える