Capture an argument passed to function in different JavaScript file using Jasmine -


i have javascript file main_one.js requires javascript file helper.js.

helper.js

warp = {   postthisevent: function(a) {     // operation on   } }; 

main_one.js

var helper = require('path/helper.js'); // steps helper.warp.postthisevent(event); 

i want capture event using jasmine. how create spy object capturing event in postthisevent()?

in jasmine test require helper, spy way:

spyon(helper.warp, "postthisevent").and.callthrough(); 

this replace postthisevent on object helper.warp spy function. when it's called spy register call , call original method instructed callthrough().

then can expect postthisevent() called expected objects way:

expect(helper.warp.postthisevent).tohavebeencalledwith(jasmine.objectcontaining({     someproperty: "somevalue" })); 

jasmine.objectcontaining() helper test expecte properties present among multiple properties of object under test.

you can inspect complex objects directly:

expect(helper.warp.postthisevent.calls.mostrecent().args[0].someproperty).tobe("somevalue"); 

note such spy might not work when postthisevent() saved variable called this:

var postthisevent = helper.warp.postthisevent; function triggering() {     postthisevent({ someproperty: "somevalue" }) } // spy created , triggering() called 

in case reference original method cannot reached when spying. there's no way intercept function/method in case in javascript.

see


Comments

Popular posts from this blog

wireshark - USB mapping with python -

c++ - nodejs socket.io closes connection before upgrading to websocket -

Deploying Qt Application on Android is really slow? -