<?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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Microsoft Technology, .Net, BizTalk, Sharepoint &#38; etc. &#187; MOQ</title>
	<atom:link href="http://littletalk.wordpress.com/tag/moq/feed/" rel="self" type="application/rss+xml" />
	<link>http://littletalk.wordpress.com</link>
	<description>Liedong(Ken) Zheng, Senior SharePoint Developer at SIMPLOT</description>
	<lastBuildDate>Tue, 22 Dec 2009 23:44:56 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='littletalk.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/273a2ea2429a815e9b5d08a05fc38987?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Microsoft Technology, .Net, BizTalk, Sharepoint &#38; etc. &#187; MOQ</title>
		<link>http://littletalk.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://littletalk.wordpress.com/osd.xml" title="Microsoft Technology, .Net, BizTalk, Sharepoint &amp; etc." />
		<item>
		<title>MOQ WCF</title>
		<link>http://littletalk.wordpress.com/2008/09/09/moq-wcf/</link>
		<comments>http://littletalk.wordpress.com/2008/09/09/moq-wcf/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 13:14:42 +0000</pubDate>
		<dc:creator>ken zheng</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[VS2008]]></category>
		<category><![CDATA[MOQ]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://littletalk.wordpress.com/?p=241</guid>
		<description><![CDATA[Here is the example you can mock a WCF service for Unit Testing
Create a simple WCF Service in VS 2008, learn more WCF from http://msdn.microsoft.com/en-au/magazine/cc163289.aspx

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=littletalk.wordpress.com&blog=2688155&post=241&subd=littletalk&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Here is the example you can mock a WCF service for Unit Testing</p>
<p>Create a simple WCF Service in VS 2008, learn more WCF from <a href="http://msdn.microsoft.com/en-au/magazine/cc163289.aspx">http://msdn.microsoft.com/en-au/magazine/cc163289.aspx</a></p>
<pre class="brush: csharp;">
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = &quot;Hello &quot;;

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }

    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format(&quot;You entered: {0}&quot;, value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite.BoolValue)
            {
                composite.StringValue += &quot;Suffix&quot;;
            }
            return composite;
        }
    }
</pre>
<p>Then create a client class which reference the service</p>
<pre class="brush: csharp;">
    public class HelloMOQ
    {

        public string getMessage(MyService.IService1 _service)
        {
            string s = &quot;&quot;;
            s = _service.GetData(12);

            return s;
        }
    }
</pre>
<p>Now it is the time to MOCK, same way to create a Unit Test Project</p>
<pre class="brush: csharp;">
        [TestMethod()]
        public void getMessageTest()
        {
            var WCFClient = new Mock&lt;IService1&gt;();
            WCFClient.Expect(client =&gt; client.GetData(1)).Returns(&quot;Hello 1&quot;);

            HelloMOQ sc = new HelloMOQ();
            sc.getMessage(WCFClient.Object);
        }
</pre>
<p>It is easy, doesn&#8217;t it.</p>
<p>You can download the source code from <a href="http://cid-5e2b95139edcd772.skydrive.live.com/self.aspx/CodeExample/MOQ|_Works.zip">Here</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/littletalk.wordpress.com/241/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/littletalk.wordpress.com/241/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/littletalk.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/littletalk.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/littletalk.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/littletalk.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/littletalk.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/littletalk.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/littletalk.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/littletalk.wordpress.com/241/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/littletalk.wordpress.com/241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/littletalk.wordpress.com/241/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=littletalk.wordpress.com&blog=2688155&post=241&subd=littletalk&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://littletalk.wordpress.com/2008/09/09/moq-wcf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e36e65741ca6b868a4f626cb656121bb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ken zheng</media:title>
		</media:content>
	</item>
		<item>
		<title>MOQ YOUR CODE</title>
		<link>http://littletalk.wordpress.com/2008/09/09/moq-your-code/</link>
		<comments>http://littletalk.wordpress.com/2008/09/09/moq-your-code/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 13:07:20 +0000</pubDate>
		<dc:creator>ken zheng</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[VS2008]]></category>
		<category><![CDATA[MOQ]]></category>
		<category><![CDATA[MOCK]]></category>
		<category><![CDATA[Readify]]></category>

		<guid isPermaLink="false">http://littletalk.wordpress.com/?p=238</guid>
		<description><![CDATA[MOQ is a very useful mocking library available to mock your code, and doesn&#8217;t require any prior knowledge or experience with mocking concepts.
You can download DLL from http://code.google.com/p/moq/
Below is the example I created to demo simple MOQ:
First, I created a couple of business classes

    public class Emailer
    {
  [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=littletalk.wordpress.com&blog=2688155&post=238&subd=littletalk&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>MOQ is a very useful mocking library available to mock your code, and doesn&#8217;t require any prior knowledge or experience with mocking concepts.</p>
<p>You can download DLL from <a href="http://code.google.com/p/moq/">http://code.google.com/p/moq/</a><br />
Below is the example I created to demo simple MOQ:<br />
First, I created a couple of business classes</p>
<pre class="brush: csharp;">
    public class Emailer
    {
        private IEmailService EmailService { get; set; }

        public Emailer(IEmailService emailService)
        {
            EmailService = emailService;
        }

        public void SendBatchEmails()
        {
            Dictionary&lt;string, string&gt; emails = new Dictionary&lt;string, string&gt;
                                                    {
                                                        {&quot;foo1@hotmail.com&quot;,&quot;foo1&quot;},
                                                        {&quot;foo2@hotmail.com&quot;,&quot;foo2&quot;},
                                                        {  &quot;foo3@hotmail.com&quot;,&quot;foo3&quot;}
                                                    };
            foreach (KeyValuePair&lt;string, string&gt; email in emails)
            {
                if (!EmailService.SendEmail(email.Key, email.Value))
                {
                    throw new ApplicationException(&quot;Some message here&quot;);
                }
            }

        }
    }

    public class EmailService: IEmailService
    {
        public bool SendEmail(string emailAddress, string message)
        {
            return false;
        }
    }

    public interface IEmailService
    {
        bool SendEmail(string emailAddress, string message);
    }
</pre>
<p>The code is quite straightforward. Now right click on the code and select &#8220;Create a Unit Test&#8221; from the context menu. A new test class is created. Create the first test method</p>
<pre class="brush: csharp;">
        [TestMethod()]
        public void NonMock()
        {
            var emailService = new EmailService();
            var emailer = new Emailer(emailService);

            emailer.SendBatchEmails();

        }
</pre>
<p>It seems do the job but actually there are a couple of issues. The first issue is this is not really Unit Test but integration test, the second one is the test method will fail as SendEmail will return false so an exception is thrown. Now we need MOQ to help us mocking the EmailService</p>
<pre class="brush: csharp;">
public void TestWithMock()
        {
            var mockEmailService = new Mock&lt;IEmailService&gt;();
            mockEmailService.Expect((x=&gt;x.SendEmail(It.IsAny&lt;string&gt;(),It.IsAny&lt;string&gt;()))).Returns(true);

            var emailer = new Emailer(mockEmailService.Object);
            emailer.SendBatchEmails();

            mockEmailService.VerifyAll();
        }
</pre>
<p>So the above code is mock the IEmailService, It.IsAny() means we don&#8217;t care about the string and the method return true. When pass the MOCK obhect remember use <strong>mockEmailService.Object</strong>. and mockEmailService.VerifyAll();</p>
<p>Below code is how to test if the expected exception when SendEmail return false and exception thrown from EmailService</p>
<pre class="brush: csharp;">
        /// &lt;summary&gt;
        ///A test for Mock which return false,
        /// expected exception thrown
        ///&lt;/summary&gt;
        [TestMethod()]
        [ExpectedException(typeof(ApplicationException), &quot;Some message here&quot;)]
        public void TestWithMock_ReturnFalse()
        {
            var mockEmailService = new Mock&lt;IEmailService&gt;();
            mockEmailService.Expect((x =&gt; x.SendEmail(It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;()))).Returns(false);

            var emailer = new Emailer(mockEmailService.Object);
            emailer.SendBatchEmails();

            mockEmailService.VerifyAll();
        }

        /// &lt;summary&gt;
        ///A test for Mock which throw exception,
        ///
        ///&lt;/summary&gt;
        [TestMethod()]
        [ExpectedException(typeof(ApplicationException), &quot;Some message here&quot;)]
        public void TestWithMock_ThrowException()
        {
            var mockEmailService = new Mock&lt;IEmailService&gt;();
            mockEmailService.Expect((x =&gt; x.SendEmail(It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;()))).Throws(new ApplicationException(&quot;AA&quot;));

            var emailer = new Emailer(mockEmailService.Object);
            emailer.SendBatchEmails();

            mockEmailService.VerifyAll();
        }
</pre>
<p>You can download code from <a href="http://cid-5e2b95139edcd772.skydrive.live.com/self.aspx/CodeExample/MOQ|_Works.zip">Here</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/littletalk.wordpress.com/238/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/littletalk.wordpress.com/238/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/littletalk.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/littletalk.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/littletalk.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/littletalk.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/littletalk.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/littletalk.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/littletalk.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/littletalk.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/littletalk.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/littletalk.wordpress.com/238/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=littletalk.wordpress.com&blog=2688155&post=238&subd=littletalk&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://littletalk.wordpress.com/2008/09/09/moq-your-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e36e65741ca6b868a4f626cb656121bb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ken zheng</media:title>
		</media:content>
	</item>
	</channel>
</rss>