开发者

Mock a web service used in an action filter

I have an external-to-my-solution web service that I'm using in an ActionFilter. The action filter grabs some basic data for my MasterPage. I've gone back and forth between using an action filter and extending the base controller class, and decided the action filter was the best approach. Then I started unit testing (Yeah, yeah TDD. Anyway... :D )

So I can't mock (using Moq, btw) a web service in an action filter beca开发者_Python百科use I can't inject my mock WS into the action filter, since action filters don't take objects as params. Right? At least that's what I seem to have come to.

Any ideas? Better approaches? I'm just trying to return a warning to the user that if the web service is unavailable, their experience might be limited.

Thanks for any help!

namespace MyProject.ActionFilters
{
    public class GetMasterPageData : ActionFilterAttribute
    {
        public ThatWS ws = new ThatWS();

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContextBase context = filterContext.HttpContext;

            try {
                DoStuff();
            }
            catch ( NullReferenceException e ) {
                context.Session["message"] = "There is a problem with the web service.  Some functionality will be limited.";
            }
        }
    }
}


Here's a quick and dirty approach:

public class GetMasterPageData : ActionFilterAttribute
{
    public Func<ISomeInterface> ServiceProvider = () => new ThatWS();

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var result = ServiceProvider().SomeMethod();
        ...
    }
}

And in your unit test you could instantiate the action filter and replace the ServiceProvider public field with some mocked object:

objectToTest.ServiceProvider = () => new SomeMockedObject();

Of course this approach is not as clean as the one suggested by @Ryan in the comments section but it could work in some situations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜