Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

174 lines (139 sloc) 4.826 kb
<!DOCTYPE html>
<meta charset="UTF-8">
<title>Doctest.js: examples</title>
<script type="text/javascript" src="../doctest.js"></script>
<script type="text/javascript" src="../.resources/toc.js"></script>
<link rel="stylesheet" type="text/css" href="../.resources/doc.css" />
<link rel="stylesheet" type="text/css" href="../doctest.css" />
<body class="autodoctest">
<!-- HEADER --><a href=""><img
style="position: absolute; top: 0; right: 0; border: 0;"
alt="Fork me on GitHub" /></a>
<div id="container">
<div class="download">
<a href="">
<img border="0" width="90"
src="" /></a>
<a href="">
<img border="0" width="90"
src="" /></a>
<h1 class="title"><a href="">doctest.js</a>
<span class="small">by
<a href="">Ian Bicking</a></span>
<!-- /HEADER -->
<h2>Doctest.js: examples</h2>
<div id="contents"></div>
<div id="doctest-output"></div>
<h3>Web Service</h3>
You can use doctest.js to test APIs; in fact, it's reasonable to use
it for acceptance tests of the web APIs themselves, not just to test
the Javascript wrappers around those APIs.
<div class="test">
In this example we'll access the <a
API</a>. First we'll want some routines to help us later on. You
could put these into a separate <code>.js</code> file and include
it, but often (especially in an example ;) it's best to be fully
transparent and list all the routines out in the open...
<pre class="doctest">
$ apiLocation = '';
$ function query(endpoint, q) {
> var url = apiLocation + endpoint;
> jQuery.ajax({
> url: url,
> data: q,
> dataType: "json",
> success: Spy('success', {wait: true, ignoreThis: true}),
> error: Spy('error')
> });
> }
Some things to notice about this example:
<li><code>apiLocation</code> is hard coded, but you could read it
from the query string, allowing something like
<li>We create a Spy for both success and failure, as we want to
track both of these. We could just use functions, but mostly
there's an advantage to being able to watch <code>.called</code>.
If you used <code>{writes: true}</code> you might not need the
<code>.applies</code> functions.
<li><code>wait</code> can be called from anywhere. That means
when you call this function doctest will wait until something is
called, and will test all the output since that time (either the
success or failure <code>writeln()</code>). Timeout is the other
Now we'll use the routine to actually run a test:
<pre class="doctest">
$ query('postalCodeSearchJSON', {postalcode: 9011, maxRows: 5});
postalCodes: [
}, ...)
<h3>Web Service/XML</h3>
<div class="test">
What we do for JSON, we can also do for XML; in this case
it's just fetching a static XML Atom document.
<pre class="doctest">
$ $.ajax({
> url: './.resources/example.xml',
> dataType: 'xml',
> success: function (doc) {
> gdoc = doc;
> writeln(repr(doc));
> },
> error: Spy('error')
> });
$ wait(0.5);
&lt;feed xmlns="">
&lt;title>Example Feed&lt;/title>
<script src=""></script>
<!-- FOOTER --></div>
You can download this project in either
<a href="">zip</a> or
<a href="">tar</a> formats.
<p>You can also clone the project with <a href="">Git</a>
by running:
<pre>$ git clone git://</pre>
<div class="footer">
get the source code on GitHub:
<a href="">ianb/doctestjs</a>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-6731441-12");
} catch(err) {}</script>
<!-- /FOOTER -->
</body> </html>
Jump to Line
Something went wrong with that request. Please try again.