<?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: Unit Testing JavaScript - Writing testable code</title>
	<atom:link href="http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/</link>
	<description></description>
	<pubDate>Fri, 18 May 2012 00:59:54 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Howard</title>
		<link>http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/comment-page-1/#comment-4849</link>
		<dc:creator>Howard</dc:creator>
		<pubDate>Thu, 25 Feb 2010 19:51:46 +0000</pubDate>
		<guid isPermaLink="false">http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/#comment-4849</guid>
		<description>One of the things that seems missing from your discussion is the
conflicting goals of privacy and testability. Something like

  (function() {
    var uniqueValue = 0;
    function next() {
      return ++uniqueValue;
    }
    Constructor = function() {
      this.getValue = next;
    }
    
  })();
  
  var instance = new Constructor();
  
has the *desirable* quality that the implementation of getValue ("next")
is hidden from outside callers. But that quality makes it pretty
difficult to mock in a test environment.</description>
		<content:encoded><![CDATA[<p>One of the things that seems missing from your discussion is the<br />
conflicting goals of privacy and testability. Something like</p>
<p>  (function() {<br />
    var uniqueValue = 0;<br />
    function next() {<br />
      return ++uniqueValue;<br />
    }<br />
    Constructor = function() {<br />
      this.getValue = next;<br />
    }</p>
<p>  })();</p>
<p>  var instance = new Constructor();</p>
<p>has the *desirable* quality that the implementation of getValue (&#8221;next&#8221;)<br />
is hidden from outside callers. But that quality makes it pretty<br />
difficult to mock in a test environment.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: matt</title>
		<link>http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/comment-page-1/#comment-3135</link>
		<dc:creator>matt</dc:creator>
		<pubDate>Wed, 03 Dec 2008 00:30:39 +0000</pubDate>
		<guid isPermaLink="false">http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/#comment-3135</guid>
		<description>Jon, I think you'll find the code in my previous post is also easy to test. In the code I adapted from your post I have tests to check that the asycCallback delegate is called.

-- also, thanks for you post, it helped me a lot in my struggle as a novice javascript coder to figure out how to test it.  I think I'm on my way now.</description>
		<content:encoded><![CDATA[<p>Jon, I think you&#8217;ll find the code in my previous post is also easy to test. In the code I adapted from your post I have tests to check that the asycCallback delegate is called.</p>
<p>&#8211; also, thanks for you post, it helped me a lot in my struggle as a novice javascript coder to figure out how to test it.  I think I&#8217;m on my way now.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: matt</title>
		<link>http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/comment-page-1/#comment-3134</link>
		<dc:creator>matt</dc:creator>
		<pubDate>Wed, 03 Dec 2008 00:26:03 +0000</pubDate>
		<guid isPermaLink="false">http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/#comment-3134</guid>
		<description>Jon, To make the class in solution 2 at all useful I think you would need to add a delegate that would be called when the function completes (successfully or with an error).  Here is my pass at it:

function MyClass()
{
    this.result = null;
    this.error = null;
    
    this.loadCustomerData = function(customerId, asyncCallback)
    {
	this.result = null;
	this.error = null;
	
        var myClass = this;
        this.makeAjaxCall(
            "POST",
            "CustomerService.asmx/LoadCustomer",
            "customerId=" + customerId,
            function(ajaxResult) { 
                myClass.result = ajaxResult;
                asyncCallback(myClass);
            },
            function(error){
            	myClass.error = error;
            	asyncCallback(myClass);
            }
         );
    }
 
    this.makeAjaxCall = function(type, url, data, successCallback, errorCallback)
    {
        $.ajax({
            type: type,
            url: url,
            data: data,
            success: function(msg){
                successCallback(msg.d); // return value from web service
            },
            success: function(req, status, error){
                errorCallback(error); // when error occurs
            }
        });
    }
}</description>
		<content:encoded><![CDATA[<p>Jon, To make the class in solution 2 at all useful I think you would need to add a delegate that would be called when the function completes (successfully or with an error).  Here is my pass at it:</p>
<p>function MyClass()<br />
{<br />
    this.result = null;<br />
    this.error = null;</p>
<p>    this.loadCustomerData = function(customerId, asyncCallback)<br />
    {<br />
	this.result = null;<br />
	this.error = null;</p>
<p>        var myClass = this;<br />
        this.makeAjaxCall(<br />
            &#8220;POST&#8221;,<br />
            &#8220;CustomerService.asmx/LoadCustomer&#8221;,<br />
            &#8220;customerId=&#8221; + customerId,<br />
            function(ajaxResult) {<br />
                myClass.result = ajaxResult;<br />
                asyncCallback(myClass);<br />
            },<br />
            function(error){<br />
            	myClass.error = error;<br />
            	asyncCallback(myClass);<br />
            }<br />
         );<br />
    }</p>
<p>    this.makeAjaxCall = function(type, url, data, successCallback, errorCallback)<br />
    {<br />
        $.ajax({<br />
            type: type,<br />
            url: url,<br />
            data: data,<br />
            success: function(msg){<br />
                successCallback(msg.d); // return value from web service<br />
            },<br />
            success: function(req, status, error){<br />
                errorCallback(error); // when error occurs<br />
            }<br />
        });<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Kruger</title>
		<link>http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/comment-page-1/#comment-3133</link>
		<dc:creator>Jon Kruger</dc:creator>
		<pubDate>Tue, 02 Dec 2008 18:40:05 +0000</pubDate>
		<guid isPermaLink="false">http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/#comment-3133</guid>
		<description>Matt,

You're correct... I just overlooked it.  I corrected the mistake.  Thanks for pointing that out.</description>
		<content:encoded><![CDATA[<p>Matt,</p>
<p>You&#8217;re correct&#8230; I just overlooked it.  I corrected the mistake.  Thanks for pointing that out.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: matt</title>
		<link>http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/comment-page-1/#comment-3132</link>
		<dc:creator>matt</dc:creator>
		<pubDate>Tue, 02 Dec 2008 18:34:49 +0000</pubDate>
		<guid isPermaLink="false">http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/#comment-3132</guid>
		<description>Jon, am I missing something in your solution 2 above? You said:  "If you have a more complicated web service that you’re running and it takes a long time to return data, you may not want to do it this way", however you are still making the jquery ajax call with "async: false".  Did you just overlook that?</description>
		<content:encoded><![CDATA[<p>Jon, am I missing something in your solution 2 above? You said:  &#8220;If you have a more complicated web service that you’re running and it takes a long time to return data, you may not want to do it this way&#8221;, however you are still making the jquery ajax call with &#8220;async: false&#8221;.  Did you just overlook that?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Kruger&#8217;s Blog &#187; Blog Archive &#187; Unit Testing JavaScript - JavaScript test frameworks</title>
		<link>http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/comment-page-1/#comment-3125</link>
		<dc:creator>Jon Kruger&#8217;s Blog &#187; Blog Archive &#187; Unit Testing JavaScript - JavaScript test frameworks</dc:creator>
		<pubDate>Wed, 26 Nov 2008 00:06:48 +0000</pubDate>
		<guid isPermaLink="false">http://jonkruger.com/blog/2008/11/25/unit-testing-javascript-writing-testable-code/#comment-3125</guid>
		<description>[...] Unit Testing JavaScript - Writing testable code [...]</description>
		<content:encoded><![CDATA[<p>[...] Unit Testing JavaScript - Writing testable code [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

