如何对自定义操作结果进行单元测试

发布于 2024-09-13 13:36:14 字数 3887 浏览 3 评论 0原文

我正在尝试对自定义操作结果进行单元测试。我最近观看了 Jimmy Bogard 的精彩 MvcConf 视频(“让你的控制器节食”)http ://www.viddler.com/explore/mvcconf/videos/1/ 并已开始尝试实现一些自定义操作结果。我已经成功地做到了这一点,ActionResult 在运行时工作正常,但我在尝试对它们进行单元测试时遇到了麻烦。

不幸的是,在代码下载中没有吉米的自定义操作方法的单元测试......这让我想知道。

我意识到操作方法只返回 ActionResult 类型的实例,并且 MVC 框架实际调用 ExecuteResult 方法,这在运行单元测试时当然不可用。因此,我的单元测试现在只是创建自定义 ActionResult 的实例,然后调用 ExecuteResult。

不幸的是,在我的自定义 ActionResult 的 ExecuteResult 方法中,它还调用我传递给它的 ViewResult 的 ExecuteResult 方法。到那时它就会爆炸。我应该如何模拟/存根这些东西才能让我的单元测试工作?

public class SendToAFriendActionResult : ActionResult
{

    public const string INVALID_CAPTCHA = "You don't appear to have filled out the two words from the security image correctly to prove you're a human. Please try again.";
    public const string INVALID_MODEL_STATE = "You don't appear to have filled out all the details correctly. Please try again.";
    public const string CONTACT_FAIL = "Unfortunately we experiend a problem sending the link. Please try again later.";
    public const string SEND_TO_A_FRIEND_FAIL_KEY = "ContactFail";

    private  RedirectResult _success;
    private  ViewResult _failure;
    private readonly SendToAFriendModel _model;
    private readonly bool _captchaValid;
    private readonly MessageBuilderServiceBase _mbs;

    public RedirectResult Success
    {
        get { return _success; }
        set { _success = value; }
    }

    public ViewResult Failure
    {
        get { return _failure; }
        set { _failure = value; }
    }

    public SendToAFriendActionResult(RedirectResult success, ViewResult failure, SendToAFriendModel model, bool captchaValid, MessageBuilderServiceBase mbs)
    {
        _success = success;
        _failure = failure;
        _model = model;
        _captchaValid = captchaValid;
        _mbs = mbs;
    }

    public override void ExecuteResult(ControllerContext context)
    {

        if (!_captchaValid)
        {
            Failure.TempData[SEND_TO_A_FRIEND_FAIL_KEY] = INVALID_CAPTCHA;

            // On reaching this point I receive the error
            // Object reference not set to an instance of an object
            // as the MVC framework calls FindView 
            Failure.ExecuteResult(context);
            return;
        }

        if (!context.Controller.ViewData.ModelState.IsValid)
        {
            Failure.TempData[SEND_TO_A_FRIEND_FAIL_KEY] = INVALID_MODEL_STATE;
            Failure.ExecuteResult(context);
            return;
        }

        _mbs.RecipientEmailAddress = _model.EmailRecipient;
        _mbs.SendersName = _model.SendersName;
        _mbs.Url = _model.URL;
        var result = _mbs.sendMessage();

        if (!result)
        {
            Failure.TempData[SEND_TO_A_FRIEND_FAIL_KEY] = CONTACT_FAIL;
            Failure.ExecuteResult(context);
            return;
        }

        Success.ExecuteResult(context);
    }
}

这是我的单元测试的开始...

        IMessageService _emailMessageSerivce;
        IGalleryRepository _repository;

        var stfModel = new SendToAFriendModel
        {
            SendersName = "Someone",
            URL = "http://someurl.com",
            EmailRecipient = "[email protected]"
        };

        var failure = new ViewResult() {ViewName ="SendToFriend"};
        const bool captchaValid = false;
        var fakeControlllerContext = MockRepository.GenerateStub<ControllerContext>(null);

        var stf = new SendToAFriendActionResult(null, failure, stfModel, captchaValid, null);
        stf.ExecuteResult(fakeControlllerContext);

我已在 SUT 中添加注释以显示问题是否发生。

我知道我应该以某种方式存根/嘲笑,但我似乎无法解决这个问题。

I'm trying to unit test a custom action result. I recently watched Jimmy Bogard's excellent MvcConf video ("put your controllers on a diet") http://www.viddler.com/explore/mvcconf/videos/1/ and have started to try and implement some custom action results. I've managed that without a problem, the ActionResult works fine at runtime but I'm having trouble trying to unit test them.

Unfortunately in the code download there are no unit tests for Jimmy's custom action methods... which make me wonder.

I realise that action methods just return instances of the ActionResult types and its the MVC framework that actually calls the ExecuteResult method, which of course is not available when running the unit test. So my unit test is now just creating an instance of my custom ActionResult and I then call ExecuteResult.

Unfortunatley in the ExecuteResult method of my custom ActionResult it is also calling the ExecuteResult method of a ViewResult that I passed it. At that point it blows up. How should I be mocking/stubbing these things to get my unit test working?

public class SendToAFriendActionResult : ActionResult
{

    public const string INVALID_CAPTCHA = "You don't appear to have filled out the two words from the security image correctly to prove you're a human. Please try again.";
    public const string INVALID_MODEL_STATE = "You don't appear to have filled out all the details correctly. Please try again.";
    public const string CONTACT_FAIL = "Unfortunately we experiend a problem sending the link. Please try again later.";
    public const string SEND_TO_A_FRIEND_FAIL_KEY = "ContactFail";

    private  RedirectResult _success;
    private  ViewResult _failure;
    private readonly SendToAFriendModel _model;
    private readonly bool _captchaValid;
    private readonly MessageBuilderServiceBase _mbs;

    public RedirectResult Success
    {
        get { return _success; }
        set { _success = value; }
    }

    public ViewResult Failure
    {
        get { return _failure; }
        set { _failure = value; }
    }

    public SendToAFriendActionResult(RedirectResult success, ViewResult failure, SendToAFriendModel model, bool captchaValid, MessageBuilderServiceBase mbs)
    {
        _success = success;
        _failure = failure;
        _model = model;
        _captchaValid = captchaValid;
        _mbs = mbs;
    }

    public override void ExecuteResult(ControllerContext context)
    {

        if (!_captchaValid)
        {
            Failure.TempData[SEND_TO_A_FRIEND_FAIL_KEY] = INVALID_CAPTCHA;

            // On reaching this point I receive the error
            // Object reference not set to an instance of an object
            // as the MVC framework calls FindView 
            Failure.ExecuteResult(context);
            return;
        }

        if (!context.Controller.ViewData.ModelState.IsValid)
        {
            Failure.TempData[SEND_TO_A_FRIEND_FAIL_KEY] = INVALID_MODEL_STATE;
            Failure.ExecuteResult(context);
            return;
        }

        _mbs.RecipientEmailAddress = _model.EmailRecipient;
        _mbs.SendersName = _model.SendersName;
        _mbs.Url = _model.URL;
        var result = _mbs.sendMessage();

        if (!result)
        {
            Failure.TempData[SEND_TO_A_FRIEND_FAIL_KEY] = CONTACT_FAIL;
            Failure.ExecuteResult(context);
            return;
        }

        Success.ExecuteResult(context);
    }
}

Here's the start of my unit test ...

        IMessageService _emailMessageSerivce;
        IGalleryRepository _repository;

        var stfModel = new SendToAFriendModel
        {
            SendersName = "Someone",
            URL = "http://someurl.com",
            EmailRecipient = "[email protected]"
        };

        var failure = new ViewResult() {ViewName ="SendToFriend"};
        const bool captchaValid = false;
        var fakeControlllerContext = MockRepository.GenerateStub<ControllerContext>(null);

        var stf = new SendToAFriendActionResult(null, failure, stfModel, captchaValid, null);
        stf.ExecuteResult(fakeControlllerContext);

I've put comments in the SUT to show were the problem occurs.

I know I should be stubbing/mocking somehow but I just can't seem to resolve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兰花执着 2024-09-20 13:36:14

来自ASP.NET MVC 2 In Action(由 Jimmy Bogard 合着):

通过去掉那些难以测试的代码
一个动作并将其放入
动作结果的执行方法,
你确保行动成为
单元测试变得更加容易。
那是因为当你对一个
动作,你断言动作的类型
操作返回的结果和
动作结果的状态。这
动作结果的执行方法
不作为单元的一部分执行
测试。

单元测试旨在隔离行为和关注点。您可以通过从自定义操作中调用 ExecuteResult 来混合关注点。相反,我会让 SendToAFriendActionResult 返回实际的 ActionResult(失败或成功):

public ActionResult GetAction(..)
{
   ActionResult result;
   //logic here to determine which ActionResult to return
   return result;
}

在您的控制器中:

  public ViewResult SendToAFriend()
    {
       return SendToAFriendActionResult(null, failure, stfModel, captchaValid, null)
            .GetAction();
    }

此方法将允许 MVC 框架完成其工作,并将这些问题隔离在您的自定义 ActionResult 之外。您的测试应该断言根据您设置的参数返回正确的操作类型(失败或成功)。

From ASP.NET MVC 2 In Action (coauthored by Jimmy Bogard):

By taking that hard-to-test code out
of an action and putting it into the
Execute method of an action result,
you ensure that the actions become
significantly easier to unit-test.
That’s because when you unit-test an
action, you assert the type of action
result that the action returns and the
state of the action result. The
Execute method of the action result
isn’t executed as part of the unit
test.

Unit tests are designed to isolate behavior and concerns. You're mixing concerns by calling ExecuteResult from within your custom Action. Instead, I would have the SendToAFriendActionResult return the actual ActionResult (Failure or Success):

public ActionResult GetAction(..)
{
   ActionResult result;
   //logic here to determine which ActionResult to return
   return result;
}

In your Controller:

  public ViewResult SendToAFriend()
    {
       return SendToAFriendActionResult(null, failure, stfModel, captchaValid, null)
            .GetAction();
    }

This method will allow the MVC framework to do its job and isolates those concerns outside your custom ActionResult. Your test should assert that the correct type of Action, failure or success, is returned based on the parameters you set going in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文