在OnActionExecuting事件中单元测试自定义属性值

时间:2017-01-05 15:40:46

标签: asp.net-mvc unit-testing moq

我在我的基本控制器中实现了以下实现,我从中获取了大部分控制器。如果没有通过控制器操作上的装饰器设置值,它会考虑为每个页面设置页面标题,元描述和关键字。

BaseController

public class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //Page title
        var title = filterContext.ActionDescriptor.GetCustomAttributes(typeof(PageTitleAttribute), false);
        if (title.Length == 1)
            ViewBag.Title = ((PageTitleAttribute)(title[0])).Parameter;
        else
            ViewBag.Title = "My website title";

        //Page keywords
        var keywords = filterContext.ActionDescriptor.GetCustomAttributes(typeof(MetaKeywordsAttribute), false);
        if (keywords.Length == 1)
            ViewBag.MetaKeywords = ((MetaKeywordsAttribute)(keywords[0])).Parameter;
        else 
            ViewBag.MetaKeywords = "targeted SEO keywords";

        //Page description
        var description = filterContext.ActionDescriptor.GetCustomAttributes(typeof(MetaDescriptionAttribute), false);
        if (description.Length == 1)
            ViewBag.MetaDescription = ((MetaDescriptionAttribute)(description[0])).Parameter;
        else 
            ViewBag.MetaDescription = "My custom description";

        base.OnActionExecuting(filterContext);
    }
}

自定义属性非常简单:

public class PageTitleAttribute : Attribute
{
    private readonly string _parameter;

    public PageTitleAttribute(string parameter)
    {
        _parameter = parameter;
    }

    public string Parameter { get { return _parameter; } }
}

public class MetaDescriptionAttribute : Attribute
{
    private readonly string _parameter;

    public MetaDescriptionAttribute(string parameter)
    {
        _parameter = parameter;
    }

    public string Parameter { get { return _parameter; } }
}

public class MetaKeywordsAttribute : Attribute
{
    private readonly string _parameter;

    public MetaKeywordsAttribute(string parameter)
    {
        _parameter = parameter;
    }

    public string Parameter { get { return _parameter; } }
}

这就是如何利用控制器中适当的动作方法的属性:

public class HomeController : BaseController
{
    [PageTitle("My new website")]
    [MetaKeywords("Explicitly set keywords")]
    [MetaDescription("description goes here")]
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Error()
    {            
        return View();
    }
}

这一切似乎都很好。现在我想创建单元测试以验证如果未通过操作方法上的属性设置值,则默认值将从基本控制器呈现为设置。我怎样才能做到这一点?我已经编写了一些测试但是我认为他们没有针对basecontroller上的filterContext。具体来说,我正在寻找没有任何属性值设置的错误操作方法的测试。仅供参考,这就是我现在设置的内容:

[TestMethod]
    public void Attribute_when_set_should_return_attribute_values() 
    {
        var method = typeof(HomeController).GetMethod("Index");
        var pageTitle = method.GetCustomAttributes(typeof(PageTitleAttribute), false)
                     .Cast<PageTitleAttribute>()
                     .SingleOrDefault();
        var keywords = method.GetCustomAttributes(typeof(MetaKeywordsAttribute), false)
                     .Cast<MetaKeywordsAttribute>()
                     .SingleOrDefault();
        var description = method.GetCustomAttributes(typeof(MetaDescriptionAttribute), false)
                     .Cast<MetaDescriptionAttribute>()
                     .SingleOrDefault();

        Assert.IsNotNull(pageTitle);
        Assert.IsNotNull(keywords);
        Assert.IsNotNull(description);

        Assert.AreEqual("My new website", pageTitle.Parameter);
        Assert.AreEqual("Explicitly set keywords", keywords.Parameter);
        Assert.AreEqual("description goes here", description.Parameter);
    }

0 个答案:

没有答案