I can't find any example of someone mocking a Dojo module successfully.
I found https://gis.utah.gov/mock-your-dojo-amd-modules-with-stubmodule-js/ but when I try to run StubModule, I get an error at line 27 of stub-module.js because "modules" isn't a member of the require variable.
I also looked into Sinon.js but they don't say anything about mocking AMD modules (http://sinonjs.org/docs/#mocks).
Does anyone know of any working examples of someone mocking a Dojo AMD module?
Based on your example, does that mean I can write something like:
require([ 'firstwatch/HelloWorld',
'dojo/promise/Promise', 'dojo/promise/all', 'firstwatch/tests/stub-module' ],
function ( HelloWorld, Promise, all, stubModule) { describe("A suite", function () {
beforeEach: function() { // run before sinon.stub(dom, 'methodInDOM').returns({ then: function(){} }); }, it("contains spec with an expectation", function () { expect(HelloWorld.deadOrAlive()).toBe("alive"); }); });});
to test the following module ("HelloWorld.js") without making the browser actually go out and make an instance of dojo/dom?
define(["dojo/dom"], function(dom) { return { saySomething: function() { alert("Hello world");
dom.doSomethingPointless(); }, deadOrAlive: function(){ return "alive"; } };});
I supposed it depends on exactly what you want to mock.
For example, you can use Sinon to stub out methods of a module, just remember to reset them so you don't taint other tests.
Something like this generator-arcgis-js-app/helpers-mapgenerator.js at master · odoe/generator-arcgis-js-app · GitHub
<SPAN class="token function">registerSuite</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN> name<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'helpers: mapgenerator'</SPAN><SPAN class="punctuation token">,</SPAN> setup<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// set up test here</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN> beforeEach<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// run before</SPAN> sinon<SPAN class="punctuation token">.</SPAN><SPAN class="token function">stub</SPAN><SPAN class="punctuation token">(</SPAN>arcgisUtils<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'createMap'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">returns</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN> then<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN> afterEach<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// run after</SPAN> arcgisUtils<SPAN class="punctuation token">.</SPAN>createMap<SPAN class="punctuation token">.</SPAN><SPAN class="token function">restore</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN> teardown<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// destroy widget</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'helper attempts to create map'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">var</SPAN> params <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN> id<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'test'</SPAN><SPAN class="punctuation token">,</SPAN> webmapid<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'1234'</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN> helper<SPAN class="punctuation token">.</SPAN><SPAN class="token function">fromWebMapAsJSON</SPAN><SPAN class="punctuation token">(</SPAN>params<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">expect</SPAN><SPAN class="punctuation token">(</SPAN>arcgisUtils<SPAN class="punctuation token">.</SPAN>createMap<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>to<SPAN class="punctuation token">.</SPAN>have<SPAN class="punctuation token">.</SPAN>been<SPAN class="punctuation token">.</SPAN>calledOnce<SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.