<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Generics and NMock</title>
	<atom:link href="http://blog.mattwynne.net/2007/10/17/generics-and-nmock/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mattwynne.net/2007/10/17/generics-and-nmock/</link>
	<description>Matt Wynne taking it one tea at a time</description>
	<lastBuildDate>Fri, 27 Jan 2012 08:31:34 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Bobby</title>
		<link>http://blog.mattwynne.net/2007/10/17/generics-and-nmock/comment-page-1/#comment-16</link>
		<dc:creator>Bobby</dc:creator>
		<pubDate>Wed, 17 Oct 2007 17:31:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.mattwynne.net/2007/10/17/generics-and-nmock/#comment-16</guid>
		<description>&lt;p&gt;Here&#039;s how its done:&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;&lt;/p&gt;

&lt;p&gt;Expect
   .Once
   .On(StubbedInvoker.fooFacade)
   .Method(new GenericMethodMatcher(&quot;GetFooFor&quot;, typeof(ExpectedBar)))
   .With(prop1).Will(Return.Value(List&lt;ExpectedBar&gt;)); &lt;/p&gt;

&lt;p&gt;GetFooFor&lt;UnExpectedBar&gt;(prop1);&lt;/p&gt;

&lt;p&gt;&lt;/pre&gt;
Here’s the matcher. Basically it uses reflection (MethodInfo). It caught us out at first as the first time it hits the matcher (when you set the expectation) as the method had not been made concrete (ContainsGenericParameters). It hits the Matcher again when the method gets called and voila the test fails.&lt;/p&gt;

&lt;pre&gt;
public class GenericMethodMatcher : Matcher
{
    private readonly string methodName;
    private readonly Type expectedType;
    private string errortext = String.Empty;

    public GenericMethodMatcher(string methodName, Type expectedType)
    {
         this.methodName = methodName;
          this.expectedType = expectedType;
    }

    public override bool Matches(object o)
    {
        MethodInfo methodInfo = o as MethodInfo;

        if (methodInfo.Name == methodName)
        {
           //if this is true it means the method has not been called yet so will not have our argument yet.
           if (methodInfo.ContainsGenericParameters)
               return true;

           foreach (Type type in methodInfo.GetGenericArguments())
           {
               if (expectedType == type)
                   return true;
               }
               errortext = string.Format(&quot;Generic method{0} called with unexpected type parameter. Expected: {1} &quot;, methodName, expectedType);
                   return false;
                }
                return false;
            }

            public override void DescribeTo(TextWriter writer)
            {
                Console.WriteLine(errortext);
            }
        }
    }
}
&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>Here&#8217;s how its done:</p>

<p><pre></pre></p>

<p>Expect
   .Once
   .On(StubbedInvoker.fooFacade)
   .Method(new GenericMethodMatcher("GetFooFor", typeof(ExpectedBar)))
   .With(prop1).Will(Return.Value(List&lt;ExpectedBar&gt;)); </p>

<p>GetFooFor&lt;UnExpectedBar&gt;(prop1);</p>

<p>
Here’s the matcher. Basically it uses reflection (MethodInfo). It caught us out at first as the first time it hits the matcher (when you set the expectation) as the method had not been made concrete (ContainsGenericParameters). It hits the Matcher again when the method gets called and voila the test fails.</p>

<pre>
public class GenericMethodMatcher : Matcher
{
    private readonly string methodName;
    private readonly Type expectedType;
    private string errortext = String.Empty;

    public GenericMethodMatcher(string methodName, Type expectedType)
    {
         this.methodName = methodName;
          this.expectedType = expectedType;
    }

    public override bool Matches(object o)
    {
        MethodInfo methodInfo = o as MethodInfo;

        if (methodInfo.Name == methodName)
        {
           //if this is true it means the method has not been called yet so will not have our argument yet.
           if (methodInfo.ContainsGenericParameters)
               return true;

           foreach (Type type in methodInfo.GetGenericArguments())
           {
               if (expectedType == type)
                   return true;
               }
               errortext = string.Format("Generic method{0} called with unexpected type parameter. Expected: {1} ", methodName, expectedType);
                   return false;
                }
                return false;
            }

            public override void DescribeTo(TextWriter writer)
            {
                Console.WriteLine(errortext);
            }
        }
    }
}
</pre>]]></content:encoded>
	</item>
</channel>
</rss>

