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

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -