javascript - jasmine.clock().tick() does not work with $timeout and debounce, but works fine with setTimeout -


Below I have 3 functions that do exactly the same thing. Each one uses a different way of calling a setTimeout, the delay 1 (), uses the settime directly, delay 2 () uses angularjs $ timeout and delays (3), uses the launches debugging All work fine.

The problem occurs when I do a test using Jasmine. SetTimeout works fine with jasmine. Clock (). Tick ​​() method, but do not have $ timeout and debugging

I am interested in doing Debian work with Jasmine, I know that I can use timeout.flush with angularjs, but $ timeout And setTimeout is giving me some other problems in my code where I'm experimenting with this leaflet map works well with debug leaflets.

I have created a plunker here: where you will see the time-limits and see the tests that are not passed when passing the settime exam.

Is there any way i can work around this problem? Thanks

JS

  var app = angular Module ('plunker', []); App.controller ('MainCtrl', function ($ radius, $ timeout) {$ scope.name = 'World'; $ scope.delayed1 = function () {setTimeout (function () {$ scope.name = "Hello World SetTimeout ";), 500)} $ scope.delayed2 = function () {$ timeout (function () {$ scope.name =" hello world timeoutout ";}, 500)} $ scope.delayed3 = function () { _.debounce (function () {$ scope.name = "Hello World by debounce"}, 500)}});  

spec

  Description ('Test a Hello World Controller', function () {var $ scope = null; var ctrl = null; // Before each module ('plunker') is required to indicate your module in a test; first (inject (function ($ Rootscope, $ controller) {$ scope = $ rootScope. $ New (); ctrl = $ controller 'MainCtrl' ($ Scope: $ scope))}}; ('world should say hello', function () (hopefully ($ scope.name). '' World '';}); ('hello world Should say by setTimeout, function () {Chameli (.) ( ); $ Scope.delayed1 (); jasmine clock () .tick (600); hopefully ($ scope.name) .toEqual ('Hello world set to timeout'); jasmine.clock () .uninstall (); }; (Hello World at the timeout ', function) (jasmine.clock (). Install (); $ scope.delayed2 (); jasmine.clock () .tick (600); hopefully ($ scope.name ) .toEqual ('Hello World at Timeout'); jasmine.clock () .Uninstall ();}) (It should be said by Hello world dee bouce ', function () {jasmine.clock (). ); $ Scope.delayed3 (); . Jasmine.clock () Tick (600); Hopefully ($ scope.name) .toEqual ('Hello world debous'); . Jasmine.clock () uninstall (); }); }); Clock only in Jasmine if you have  setInterval)  

Code> setTimeout () function to run this synchronization directly as it unnecessarily decomposes those tasks. I believe there is a duplicate request for Jasmin's date object, which is a _. Debounce () allows for such tasks, but I do not remember whether it has been merged or not.

To test _ debounce () you have to copy it to run synchronize, preferably as a detective or you can override the function. It has got to work for me:

  on spy (_, 'debor'). And .callfake (function (funk) {return function () {funny.apps (this, logic);};});  

Now call _. Debounce () will run synchronously and the tests will be completed successfully. Of course you still have to use $ timeout.flush () .

I have updated my planer with these changes:


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -