<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>Martin Normark&#039;s blog &#187; Unit testing</title>
	<atom:link href="http://martinnormark.com/tag/unit-testing/feed" rel="self" type="application/rss+xml" />
	<link>http://martinnormark.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 21:06:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Unit Testing ASP.NET MVC Controller Action contains specific ActionFilter Attributes</title>
		<link>http://martinnormark.com/unit-testing-asp-net-mvc-controller-action-contains-actionfilter?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/unit-testing-asp-net-mvc-controller-action-contains-actionfilter#comments</comments>
		<pubDate>Tue, 15 May 2012 21:06:09 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Unit testing]]></category>
		<category><![CDATA[ActionFilters]]></category>

		<guid isPermaLink="false">http://martinnormark.com/?p=424</guid>
		<description><![CDATA[ActionFilters in ASP.NET MVC are great. You can now easily share logic between controllers without having to inherit from a base controller, that does the common work. I have a content heavy application that supports a set of layouts. Each layout &#8230; <p><a class="btn small" href="http://martinnormark.com/unit-testing-asp-net-mvc-controller-action-contains-actionfilter">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/dd410209.aspx">ActionFilters</a> in ASP.NET MVC are great.</p>
<p>You can now easily share logic between controllers without having to inherit from a base controller, that does the common work.</p>
<p>I have a content heavy application that supports a set of layouts. Each layout is rendered by setting the Layout view dynamically, which I do from an ActionFilter. My ActionFilter is fully tested, but when you delegate work to an ActionFilter, you should write a test that ensures the filter is defined on the action.</p>
<p>Not surprisingly, this is very simple and relies on reflection (Download from Gist: <a href="https://gist.github.com/2605628">https://gist.github.com/2605628</a>):</p>
<pre class="prettyprint">/// &lt;summary&gt;
/// Verifies the controller action, contains an attribute of the specified attributeType.
/// &lt;/summary&gt;
/// &lt;param name="controller"&gt;The controller.&lt;/param&gt;
/// &lt;param name="action"&gt;The action method.&lt;/param&gt;
/// &lt;param name="attributeType"&gt;Type of the attribute to look for.&lt;/param&gt;
/// &lt;returns&gt;Returns true if the attribute was present on the action. Otherwise false.&lt;/returns&gt;
public static bool VerifyControllerActionAttribute(this Controller controller, Func&lt;ActionResult&gt; action, Type attributeType)
{
    MethodInfo methodInfo = action.Method;
    object[] attributes = methodInfo.GetCustomAttributes(attributeType, true);
    return attributes.Any(a =&gt; a.GetType() == attributeType);
}</pre>
<p>And the usage looks like this:</p>
<pre class="prettyprint">[TestMethod]
public void Index_ContainsTemplateLayoutAttribute()
{
    HomeController controller = new HomeController();
    bool containsAttribute = controller.VerifyControllerActionAttribute(controller.Index, typeof(TemplateLayoutAttribute));
    Assert.IsTrue(containsAttribute);
}</pre>
<p>Need an introduction? Watch a video about <a href="http://vimeo.com/32617893">unit testing ASP.NET and MVC based web apps</a> on Vimeo, published by Typemock.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/unit-testing-asp-net-mvc-controller-action-contains-actionfilter/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Unit test for verifying references from DataAnnotation validation to the ErrorMessageResourceName value</title>
		<link>http://martinnormark.com/unit-test-for-verifying-references-from-dataannotation-validation-to-the-errormessageresourcename-value?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/unit-test-for-verifying-references-from-dataannotation-validation-to-the-errormessageresourcename-value#comments</comments>
		<pubDate>Thu, 04 Mar 2010 17:52:00 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Internationalization]]></category>
		<category><![CDATA[Unit testing]]></category>

		<guid isPermaLink="false">/post/Unit-test-for-verifying-references-from-DataAnnotation-validation-to-the-ErrorMessageResourceName-value.aspx</guid>
		<description><![CDATA[I love the new model validation features in System.ComponentModel.DataAnnotations. One thing I don’t like though, is that the ErrorMessageResourceName is loosely typed. The ErrorMessageResourceType, however, is a System.Type which will be strongly typed by assigning its value using the typeof(Namespace.ResourceSetType) &#8230; <p><a class="btn small" href="http://martinnormark.com/unit-test-for-verifying-references-from-dataannotation-validation-to-the-errormessageresourcename-value">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>I love the new model validation features in System.ComponentModel.DataAnnotations. One thing I don’t like though, is that the ErrorMessageResourceName is loosely typed. The ErrorMessageResourceType, however, is a System.Type which will be strongly typed by assigning its value using the typeof(Namespace.ResourceSetType) method.</p>
<p>Since there’s no build-breaking reference between a resource file and the value of the ErrorMessageResourceName on all classes where you use it, I thought it would be cool to have a unit test that verifies the existence of all referenced resource keys.</p>
<p>Remember to add a reference to System.ComponentModel.DataAnnotations.</p>
<h2>Code</h2>
<pre class="csharpcode"><span class="rem">/// &lt;summary&gt;</span>
<span class="rem">/// Verifies that all properties that are decorated with validation data-annotations, refers to </span>
<span class="rem">/// an existing resource. This will make sure, that missing resources are not referenced.</span>
<span class="rem">/// &lt;/summary&gt;</span>
[TestMethod]
<span class="kwrd">public</span> <span class="kwrd">void</span> All_Properties_With_Validation_Annotations_Must_Refer_To_Existing_Resource()
{
    Assembly assembly = Assembly.Load(<span class="kwrd">new</span> AssemblyName(<span class="str">"MyApp.Model.Namespace"</span>));
    var types = assembly.GetTypes().Where&lt;Type&gt;(t =&gt; t.IsClass &amp;&amp; !t.IsAbstract);

    <span class="kwrd">foreach</span> (var type <span class="kwrd">in</span> types)
    {
        var properties = type.GetProperties();

        <span class="kwrd">foreach</span> (PropertyInfo property <span class="kwrd">in</span> properties)
        {
            var attributes = property.GetCustomAttributes(<span class="kwrd">true</span>);

            <span class="kwrd">foreach</span> (var item <span class="kwrd">in</span> attributes)
            {
                <span class="kwrd">if</span> (item <span class="kwrd">is</span> ValidationAttribute)
                {
                    ValidationAttribute val = item <span class="kwrd">as</span> ValidationAttribute;

                    Assert.IsNotNull(val);

                    <span class="kwrd">if</span> (val.ErrorMessageResourceType != <span class="kwrd">null</span>)
                    {
                        Assert.AreNotEqual(String.Empty, val.ErrorMessageResourceName,
                            String.Format(<span class="str">@"Validation Error Resource specified on property:
                        {0}.{1} is empty!"</span>, type.ToString(), property.Name));

                        <span class="kwrd">try</span>
                        {
                            ResourceManager rm = <span class="kwrd">new</span> ResourceManager(val.ErrorMessageResourceType);
                            <span class="kwrd">string</span> resourceValue = rm.GetString(val.ErrorMessageResourceName);
                            Assert.IsFalse(String.IsNullOrEmpty(resourceValue),
                                String.Format(<span class="str">@"The value of the Validation Error Resource specified on property:
                            {0}.{1} is empty!"</span>, type.ToString(), property.Name));
                        }
                        <span class="kwrd">catch</span> (MissingManifestResourceException)
                        {
                            Assert.Fail(String.Format(<span class="str">@"Validation Error Resource specified on property:
                            {0}.{1} could not be found!"</span>, type.ToString(), property.Name));
                        }
                    }
                }
            }
        }
    }
}</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/unit-test-for-verifying-references-from-dataannotation-validation-to-the-errormessageresourcename-value/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

