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
- jasmine 2.4 spies and
- spy inspection in chapter other tracking properties
Comments
Post a Comment