<?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/"
	>

<channel>
	<title>Test Driven Development &#8211; blog.boro2g .co.uk</title>
	<atom:link href="https://blog.boro2g.co.uk/category/tdd/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.boro2g.co.uk</link>
	<description>Some ideas about coding, dev and all things online.</description>
	<lastBuildDate>Mon, 19 Dec 2016 10:26:20 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.8</generator>
	<item>
		<title>Jasmine tests for jquery post</title>
		<link>https://blog.boro2g.co.uk/jasmine-tests-jquery-post/</link>
					<comments>https://blog.boro2g.co.uk/jasmine-tests-jquery-post/#respond</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Fri, 27 Jun 2014 16:30:43 +0000</pubDate>
				<category><![CDATA[Test Driven Development]]></category>
		<guid isPermaLink="false">http://blog.boro2g.co.uk/?p=424</guid>

					<description><![CDATA[<p>Jasmine is a js framework that allows behaviour driven testing of Javascript components. One challenge I ran into was how to setup the tests in order to mock jQuery.post(&#8230;) calls. An example of the code for making the call is: [crayon-69b25605ed051154990637/] So, how do you test this and validate the correct url &#038; data was [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/jasmine-tests-jquery-post/">Jasmine tests for jquery post</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a title="Jasmine" href="http://jasmine.github.io/" target="_blank">Jasmine</a> is a js framework that allows behaviour driven testing of Javascript components. One challenge I ran into was how to setup the tests in order to mock jQuery.post(&#8230;) calls.</p>
<p>An example of the code for making the call is:</p>
<p></p><pre class="crayon-plain-tag">$.post(url, JSON.stringify({ &quot;value&quot;: &quot;1&quot;, &quot;another_value&quot;: true })
).done(function (data) {
    handleApiResponse(data);
}).fail(function (data) {
    handleFailedApiResponse(data);
});</pre><p></p>
<p>So, how do you test this and validate the correct url &#038; data was used? </p>
<p>First, you need to spy on the ajax call:</p><pre class="crayon-plain-tag">spyOn($, &quot;ajax&quot;).andCallFake(function (options) {
    var d = $.Deferred();
    d.resolve({});
    return d.promise();
});</pre><p><em>The interesting thing to note, even though you are calling $.post, you need to spy on $.ajax</em><br />
If you want to force the .fail() state of the code then you can use d.reject(). See <a href="http://api.jquery.com/category/deferred-object/" title="http://api.jquery.com/category/deferred-object/">http://api.jquery.com/category/deferred-object/</a> for more alternatives.</p>
<p>When you then verify the valid behaviour, you can check eg:</p><pre class="crayon-plain-tag">var data = { &quot;value&quot;: &quot;1&quot;, &quot;another_value&quot;: true };
...
expect($.ajax.mostRecentCall.args[0][&quot;type&quot;].toUpperCase()).toEqual(&quot;POST&quot;);
expect($.ajax.mostRecentCall.args[0][&quot;url&quot;]).toEqual(url);
if (data) {
    expect($.ajax.mostRecentCall.args[0][&quot;data&quot;]).toEqual(data);
}</pre><p></p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/jasmine-tests-jquery-post/">Jasmine tests for jquery post</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/jasmine-tests-jquery-post/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Moq &#8211; the basics</title>
		<link>https://blog.boro2g.co.uk/moq-the-basics/</link>
					<comments>https://blog.boro2g.co.uk/moq-the-basics/#respond</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Wed, 04 Apr 2012 20:37:38 +0000</pubDate>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<guid isPermaLink="false">http://blog.boro2g.co.uk/?p=185</guid>

					<description><![CDATA[<p>During a recent XP programming course we made use of the Moq&#8217;ing framework. I&#8217;d not used this before so tried to come up with some example moq basic tests. There are several tutorials on the internet which offer up the basics so do hunt around. Hopefully the following provides some simple examples for people to [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/moq-the-basics/">Moq &#8211; the basics</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>During a recent XP programming course we made use of the Moq&#8217;ing framework. I&#8217;d not used this before so tried to come up with some example moq basic tests. There are several tutorials on the internet which offer up the basics so do hunt around. Hopefully the following provides some simple examples for people to get started with.</p>
<p>The following code relies on the following assemblies:</p>
<ul>
<li>nunit.framework &#8211; available from <a title="http://www.nunit.org/" href="http://www.nunit.org/" target="_blank">http://www.nunit.org/</a></li>
<li>moq &#8211; available from <a title="http://code.google.com/p/moq/" href="http://code.google.com/p/moq/" target="_blank">http://code.google.com/p/moq/</a></li>
</ul>
<p>The real crux of TDD is that you program your functionality to interfaces &#8211; here we have a simple interface and dto:</p><pre class="crayon-plain-tag">public interface IMyInterface
{
    int IntMethod(int value);

    int HarderIntMethod(int value, int anotherValue);

    MyObject MyObjectMethod(int value);
}

public class MyObject
{
    public virtual string MyVirtualProperty { get; set; }
    public string MyProperty { get; set; }
}</pre><p><em>Note the importance of the virtual property in MyObject will be highlighted in the tests</em>. In the following examples not all the tests pass &#8211; this is intentional! Failing tests are included to demonstrate specific features of Moq. Apologies for the long dump of test code &#8211; the idea is really to copy it into a test class and let NUnit do the work.</p><pre class="crayon-plain-tag">[TestFixture]
public class MyTests
{
    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    public void A_TestMockBehaviourDefault(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;();

        int result = myInterface.Object.IntMethod(value);

        //we havent setup mock for value = 1
        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    public void B_TestMockBehaviourStrict(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        int result = myInterface.Object.IntMethod(value);

        //now in strict mode
        //again, we havent setup mock for value = 1
        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void C_TestMockBehaviourStrictWithSpecificValue(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        myInterface.Setup(a =&gt; a.IntMethod(1)).Returns(1);

        int result = myInterface.Object.IntMethod(value);

        //we have setup the mock for value = 1 but not value = 2
        Assert.AreEqual(value, result);

        myInterface.Verify();
    }       

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void D_TestMockBehaviourStrictWithTypedValue(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Returns((int a) =&gt; a);

        int result = myInterface.Object.IntMethod(value);

        //any int will pass
        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    private int _count = 0;

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void E_TestMockBehaviourStrictWithTypedValueAndCallback(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);            

        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Returns((int a) =&gt; a).Callback(() =&gt; _count++);

        int result = myInterface.Object.IntMethod(value);

        //we can log callbacks before or after the Returns call
        Console.WriteLine(String.Format(&quot;Called E_TestMockBehaviourStrictWithTypedValueAndCallback {0} times&quot;, _count));

        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void E1_TestMockBehaviourStrictWithTypedValueAndCallbackBefore(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //callbacks can happen before and after the Returns call
        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Callback(() =&gt; Console.WriteLine(&quot;before&quot;)).Returns((int a) =&gt; a).Callback(() =&gt; Console.WriteLine(&quot;after&quot;));

        int result = myInterface.Object.IntMethod(value);

        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    [ExpectedException(typeof(Exception))]
    public void F_TestMockBehaviourStrictWithException(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //this can throw specific exceptions if we want
        //catching at a test level is a bit loose since other code could exception - see F1 for alternative
        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Throws&lt;Exception&gt;();

        int result = myInterface.Object.IntMethod(value);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void F1_TestMockBehaviourStrictWithException(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //this can throw specific exceptions if we want
        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Throws&lt;Exception&gt;();

        //here we can catch specific exceptions
        Assert.Throws&lt;Exception&gt;(() =&gt; myInterface.Object.IntMethod(value));

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void G_TestMockBehaviourStrictVerify(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Returns((int a) =&gt; a).Verifiable();
        myInterface.Setup(a =&gt; a.HarderIntMethod(It.IsAny&lt;int&gt;(), (It.IsAny&lt;int&gt;()))).Returns((int a, int b) =&gt; a).Verifiable();

        int result = myInterface.Object.IntMethod(value);

        //we havent called HarderIntMethod
        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void H_TestMockBehaviourStrictVerifySuccess(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        myInterface.Setup(a =&gt; a.IntMethod(It.IsAny&lt;int&gt;())).Returns((int a) =&gt; a).Verifiable();
        myInterface.Setup(a =&gt; a.HarderIntMethod(It.IsAny&lt;int&gt;(), It.IsAny&lt;int&gt;())).Returns((int a, int b) =&gt; a).Verifiable();

        int result = myInterface.Object.IntMethod(value);

        int anotherResult = myInterface.Object.HarderIntMethod(value, value);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void I_TestMockBehaviourStrictWithTypedValueAndLogicOnInput(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //setup can perform logic based on input to determine return value(s)
        myInterface.Setup(a =&gt; a.IntMethod(It.Is&lt;int&gt;(b =&gt; b % 2 == 0))).Returns((int a) =&gt; a);
        myInterface.Setup(a =&gt; a.IntMethod(It.Is&lt;int&gt;(b =&gt; b % 2 != 0))).Returns((int a) =&gt; -a);

        int result = myInterface.Object.IntMethod(value);

        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    [TestCase(4, TestName = &quot;4&quot;)]
    public void J_TestMockBehaviourStrictWithTypedValueAndRangeLogicOnInput(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //rather than specific logic, instead we can make use of Ranges
        myInterface.Setup(a =&gt; a.IntMethod(It.IsInRange&lt;int&gt;(0,3, Range.Inclusive))).Returns((int a) =&gt; a);

        int result = myInterface.Object.IntMethod(value);

        Assert.AreEqual(value, result);

        //we haven't setup a return value for 4
        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void K_TestMockBehaviourStrictWithProperties(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //we can manipulate objects being returned
        myInterface.Setup(a =&gt; a.MyObjectMethod(value)).Returns((int a) =&gt; new MyObject() { MyProperty = a.ToString() });

        MyObject result = myInterface.Object.MyObjectMethod(value);

        Assert.AreEqual(value.ToString(), result.MyProperty);

        myInterface.Verify();
    }

    [Test]
    public void L_TestMockBehaviourStrictWithPropertiesVerifyGet()
    {
        Mock&lt;MyObject&gt; myObject = new Mock&lt;MyObject&gt;(MockBehavior.Strict);

        //this is where you need virtual properties
        myObject.Setup(a =&gt; a.MyProperty).Returns(&quot;blah&quot;);

        myObject.Verify();

        myObject.VerifyGet(a =&gt; a.MyProperty);
    }

    [Test]
    public void L1_TestMockBehaviourStrictWithPropertiesVerifyGet()
    {
        Mock&lt;MyObject&gt; myObject = new Mock&lt;MyObject&gt;(MockBehavior.Strict);

        myObject.Setup(a =&gt; a.MyVirtualProperty).Returns(&quot;blah&quot;);

        myObject.Verify();

        //we have never accessed MyVirtualProperty hence the VerifyGet fails
        myObject.VerifyGet(a =&gt; a.MyVirtualProperty);
    }

    [Test]
    public void L2_TestMockBehaviourStrictWithPropertiesVerifyGet()
    {
        Mock&lt;MyObject&gt; myObject = new Mock&lt;MyObject&gt;(MockBehavior.Strict);

        myObject.Setup(a =&gt; a.MyVirtualProperty).Returns(&quot;blah&quot;);

        Assert.IsNotEmpty(myObject.Object.MyVirtualProperty);

        myObject.Verify();

        //we have accessed both properties
        myObject.VerifyGet(a =&gt; a.MyVirtualProperty);
    }

    [Test]
    public void L3_TestMockBehaviourStrictWithPropertiesSetupProperty()
    {
        Mock&lt;MyObject&gt; myObject = new Mock&lt;MyObject&gt;(MockBehavior.Strict);

        //rather than setup and then adding Returns method, SetupProperty achieves the same thing
        myObject.SetupProperty(a =&gt; a.MyVirtualProperty, &quot;blah&quot;);

        Assert.IsNotEmpty(myObject.Object.MyVirtualProperty);

        myObject.Verify();

        myObject.VerifyGet(a =&gt; a.MyVirtualProperty);
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void M_TestMockBehaviourStrictWithTypedValueAndDetailedCallback(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //Callback and Returns can both deal with multiple parameters via Generics or parameters.
        // M1 shows the opposite of each
        myInterface.Setup(a =&gt; a.HarderIntMethod(It.IsAny&lt;int&gt;(), It.IsAny&lt;int&gt;()))
            .Returns((int a, int b) =&gt; a)
            .Callback&lt;int, int&gt;((a, b) =&gt; Console.WriteLine(&quot;A is &quot; + a + &quot; B is &quot; + b));

        int result = myInterface.Object.HarderIntMethod(value, value + 1);

        Assert.AreEqual(value, result);

        myInterface.Verify();
    }

    [Test]
    [TestCase(1, TestName = &quot;1&quot;)]
    [TestCase(2, TestName = &quot;2&quot;)]
    public void M1_TestMockBehaviourStrictWithTypedValueAndDetailedCallbackAlternativeSyntax(int value)
    {
        Mock&lt;IMyInterface&gt; myInterface = new Mock&lt;IMyInterface&gt;(MockBehavior.Strict);

        //demonstrates the opposite of M in terms of the syntax required into Callback and Returns
        myInterface.Setup(a =&gt; a.HarderIntMethod(It.IsAny&lt;int&gt;(), It.IsAny&lt;int&gt;()))
            .Returns&lt;int , int&gt;((a, b) =&gt; a)
            .Callback((int a, int b) =&gt; Console.WriteLine(&quot;A is &quot; + a + &quot; B is &quot; + b));

        int result = myInterface.Object.HarderIntMethod(value, value + 1);

        Assert.AreEqual(value, result);

        myInterface.Verify();
    }
}</pre><p>Here is the expected results when evaluated in NUnit:</p>
<p><a href="http://blog.boro2g.co.uk/wp-content/uploads/2012/04/nunit.jpg"><img fetchpriority="high" decoding="async" class="alignnone size-medium wp-image-193" title="nunit" src="http://blog.boro2g.co.uk/wp-content/uploads/2012/04/nunit-300x230.jpg" alt="" width="300" height="230" srcset="https://blog.boro2g.co.uk/wp-content/uploads/2012/04/nunit-300x230.jpg 300w, https://blog.boro2g.co.uk/wp-content/uploads/2012/04/nunit-1024x787.jpg 1024w, https://blog.boro2g.co.uk/wp-content/uploads/2012/04/nunit.jpg 1280w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/moq-the-basics/">Moq &#8211; the basics</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/moq-the-basics/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
