最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

I have a JavaScript file main_one.js which requires another JavaScript file helper.js.

helper.js

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

main_one.js

var Helper = require('path/helper.js');
// some steps
Helper.warp.postThisEvent(event);

I want to capture event using Jasmine. How do I create my spy object for capturing event in postThisEvent()?

I have a JavaScript file main_one.js which requires another JavaScript file helper.js.

helper.js

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

main_one.js

var Helper = require('path/helper.js');
// some steps
Helper.warp.postThisEvent(event);

I want to capture event using Jasmine. How do I create my spy object for capturing event in postThisEvent()?

Share Improve this question edited May 9, 2016 at 8:31 jotik 17.9k16 gold badges65 silver badges129 bronze badges asked May 9, 2016 at 8:09 tourniquet_grabtourniquet_grab 91110 silver badges15 bronze badges 2
  • Do you want to do anything with event, or just prove that it has been called with event? What are you trying to prove in the test? Are you using event for anything in the test? – Shakespeare Commented May 9, 2016 at 8:35
  • The event is being sent to external code. The event has a number of properties. I want to check that all the required properties are set before sending the event. – tourniquet_grab Commented May 9, 2016 at 19:57
Add a comment  | 

1 Answer 1

Reset to default 19

In the Jasmine test require Helper, then spy this way:

spyOn(Helper.warp, "postThisEvent").and.callThrough();

This will replace postThisEvent on the object Helper.warp with a spy function. When it's called the spy will register the call and than call the original method as this was instructed by callThrough().

Then you can expect that postThisEvent() was called with the expected objects this way:

expect(Helper.warp.postThisEvent).toHaveBeenCalledWith(jasmine.objectContaining({
    someProperty: "someValue"
}));

jasmine.objectContaining() is a helper that will only test that the expecte properties are present among multiple properties of the object under test.

You can also inspect complex objects directly:

expect(Helper.warp.postThisEvent.calls.mostRecent().args[0].someProperty).toBe("someValue");

Note that such a spy might not work when postThisEvent() was saved to a variable which is then called like this:

var postThisEvent = Helper.warp.postThisEvent;
function triggering() {
    postThisEvent({ someProperty: "someValue" })
}
// now a spy is created and then triggering() is called

In this case the reference to the original method cannot be reached when spying. There's no way to intercept the function/method in this case in Javascript.

See

  • Jasmine 2.4 Spies and
  • Spy inspection in the chapter Other tracking properties
发布评论

评论列表(0)

  1. 暂无评论