【问题标题】:RhinoMocks Testing a ServiceRhinoMocks 测试服务
【发布时间】:2012-09-11 19:57:32
【问题描述】:

我在 NUnit 中使用 Sharp Architechture 和 Rhino Mocks。

我有一个看起来像这样的测试服务

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



    }

感谢任何帮助。

【问题讨论】:

    标签: c# asp.net-mvc-3 rhino-mocks nunit-2.5.9


    【解决方案1】:

    看起来您尝试编写的不是单元测试。它更像是集成测试。遵循单元测试思想,您有两个单元:ServiceController。这个想法是您应该分别测试每个单元并保持测试简单。首先,您应该为TestService 编写测试。之后,当你覆盖它时,使用 Stubs/Mocks 为 Controller 编写测试 TestService。所以你对Controller 的测试看起来是对的,它根据Service.Create 方法的结果测试重定向是否发生。您应该在没有控制器上下文的情况下为您的 TestService 添加测试,并且它有很好的覆盖范围。 如果你想一起测试这些单元,那么你不能使用模拟,它更像是集成测试。 此外,为了涵盖模块之间的集成,您可以使用 WatiN 或 Selenium 等工具编写基于 Web 的测试来测试整个应用程序。 但无论如何,为单独的部分编写单元测试是一个好习惯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 2017-10-04
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多