Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming you want <strong>Unit Tests</strong> for your adapter code, like the title of the question specified, I would note the following:</p> <ul> <li><p>The scope of Unit Tests are a single unit of code, in JavaScript that usually means a small function. You test if <code>sum(1,2)</code> returns <code>3</code>. You do <strong>not</strong> automatically open a URL that shows your calculator application, simulates user input to click on buttons (<code>1</code>, <code>2</code> and <code>=</code>), waits for the calculation event to return <code>3</code> and the test code reads the output from the DOM. The former describes a Unit Test, the latter describes an Integration/Functional Test. If you want Functional Tests, read <a href="http://pic.dhe.ibm.com/infocenter/wrklight/v6r0m0/topic/com.ibm.worklight.help.doc/what_s_new/c_wn_functional_testing.html" rel="nofollow">this</a>. If you want Unit Tests, keep reading.</p></li> <li><p>Mock everything related to third-party APIs (e.g. Worklight, jQuery, Dojo). These APIs have already been tested and are known to work, at the very least that's a reasonable assumption to make. For example, when you write JUnit tests you assume everything from the standard Java library works as specified in the documentation. <a href="http://sinonjs.org/" rel="nofollow">Sinon.js</a> is a great library for creating JavaScript mocks, stubs and spies.</p></li> <li><p>Use either <a href="https://developer.mozilla.org/en-US/docs/Rhino/Examples" rel="nofollow">Mozilla Rhino</a> or <a href="http://nodejs.org/" rel="nofollow">Node.js</a> to read and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval" rel="nofollow">eval</a> the JavaScript for your adapter's JavaScript implementation file (e.g. <code>myCalculatorAdapter-impl.js</code>). There are many good ways to create the assertions, for example <a href="http://visionmedia.github.io/mocha/" rel="nofollow">Mocha</a> if you go the Node.js route.</p></li> </ul> <p>Here's some example code, imagine this is my adapter procedure:</p> <p><strong>myCalculatorAdapter-impl.js</strong></p> <pre><code>function sum (a, b) { WL.Logger.debug({hello: 'world'}); return a+b; } </code></pre> <p>Imagine the following contains my unit tests for said adapter procedure:</p> <p><strong>test.js</strong></p> <pre><code>//Mocks var WL = {}; WL.Logger = {}; WL.Logger.debug = function (msg) { console.log(msg); }; //Node.js Libraries var fs = require('fs'), assert = require('assert'); //Read the adapter code eval(fs.readFileSync('./myCalculatorAdapter-impl.js').toString()); //Do assertions -- Unit testing assert.equal(sum(1, 2), 3, '1+2 should be 3'); </code></pre> <p>I run it with: <code>node test.js</code>. If I change the implementation to subtract instead of add (<code>return a-b</code>) when I run it I'm alerted of the failure <code>AssertionError: 1+2 should be 3</code>.</p> <p>I used the <a href="http://nodejs.org/api/assert.html" rel="nofollow">standard assertion</a> library that Node.js provides, you may want to use something with more features. Similarly you may want to use a library for mocking third party APIs or things that don't apply to the current test. If you're doing network communication, you will want to mock the inputs.</p> <p>Surely there are others ways to test adapters, feel free to experiment and share. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload