Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion spying on requireJS's require within an AMD module

Date: Fri, 9 Nov 2012 01:16:57 -0800 (PST)
From: Gavin Jackson <gav.jacks...@gmail.com>
To: jasmine-js@googlegroups.com
Message-Id: <8f45c341-0651-41ff-bc2e-65f01bd132f1@googlegroups.com>
In-Reply-To: <a067788d-5102-4782-8880-4bcad440f89f@googlegroups.com>
References: <a067788d-5102-4782-8880-4bcad440f89f@googlegroups.com>
Subject: Re: spying on requireJS's require within an AMD module
MIME-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_514_13618255.1352452617390"

------=_Part_514_13618255.1352452617390
Content-Type: multipart/alternative; 
	boundary="----=_Part_515_7231744.1352452617390"

------=_Part_515_7231744.1352452617390
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

I figured this out, FYI it turns out all that was needed to be done was to 
spy on window.  This is more a CoffeeScript thing than anything else, as 
thats the only way CoffeeScript can access globals (what with not having 
`var`)

spyOn(window, 'require').andCallFake (dependencies, callBack, errorCallBack) 
->

  errorCallBack.call()

Hope someone finds that useful

On Thursday, 8 November 2012 10:48:06 UTC, Gavin Jackson wrote:
>
> Hi
>
> I am using RequireJS to load AMD modules on demand inside of an AMD module 
> already loaded using RequireJS.  I want to mock the `require()` call which 
> is part of my testable code. 
>
> My class under test looks like:
>
> define ['some/dependencies/here'], (dependency) ->
>   class ClassUnderTest
>     ..
>     ..
>     _addWidget: (payload) ->
>         require [payload.dependency], (dependency) ->
>           #stuff i want to test in here
>
> Cheers 
>
> Gavin
>

------=_Part_515_7231744.1352452617390
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

I figured this out, FYI it turns out all that was needed to be done was to =
spy on window. &nbsp;This is more a CoffeeScript thing than anything else, =
as thats the only way CoffeeScript can access globals (what with not having=
 `var`)<div><br></div><div>







<p class=3D"p1"><font face=3D"courier new, monospace">spyOn<span class=3D"s=
1">(</span>window<span class=3D"s1">, </span><span class=3D"s2">'require'</=
span><span class=3D"s1">).</span>andCallFake<span class=3D"s1"> (</span>dep=
endencies<span class=3D"s1">, </span>callBack<span class=3D"s1">, </span>er=
rorCallBack<span class=3D"s1">) </span><span class=3D"s3">-&gt;</span></fon=
t></p><p class=3D"p1"><font face=3D"courier new, monospace">&nbsp; errorCal=
lBack<span class=3D"s1">.</span>call<span class=3D"s1">()</span></font></p>=
<div><br></div><div>Hope someone finds that useful</div><br>On Thursday, 8 =
November 2012 10:48:06 UTC, Gavin Jackson  wrote:<blockquote class=3D"gmail=
_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;p=
adding-left: 1ex;">Hi<div><br></div><div>I am using RequireJS to load AMD m=
odules on demand inside of an AMD module already loaded using RequireJS. &n=
bsp;I want to mock the `require()` call which is part of my testable code.&=
nbsp;</div><div><br></div><div>My class under test looks like:</div><div><b=
r></div><div><font face=3D"courier new, monospace">define ['some/dependenci=
es/here'], (dependency) -&gt;</font></div><div><font face=3D"courier new, m=
onospace">&nbsp; class ClassUnderTest</font></div><div><font face=3D"courie=
r new, monospace">&nbsp; &nbsp; ..</font></div><div><font face=3D"courier n=
ew, monospace">&nbsp; &nbsp; ..</font></div><div><font face=3D"courier new,=
 monospace">&nbsp; &nbsp; _addWidget: (payload) -&gt;</font></div><div><fon=
t face=3D"courier new, monospace">&nbsp; &nbsp; &nbsp; &nbsp; require [payl=
oad.</font><span style=3D"font-family:'courier new',monospace">dependency</=
span><font face=3D"courier new, monospace">], (dependency) -&gt;</font></di=
v><div><font face=3D"courier new, monospace">&nbsp; &nbsp; &nbsp; &nbsp; &n=
bsp; #stuff i want to test in here</font></div><div><br></div><div>Cheers&n=
bsp;</div><div><br></div><div>Gavin</div></blockquote></div>
------=_Part_515_7231744.1352452617390--

------=_Part_514_13618255.1352452617390--