문제

I have Ruby on Rails application and in my application.js file there is:

jQuery(function($) {
    submitFormForBlacklistsCsvExport();
}

function submitFormForBlacklistsCsvExport(){
  jQuery('#blacklists_csv_export').bind('click', function(){
   exportBlacklists('/whitelists')
  })
}

I am using jasmine for testing of javascript in the app but I couldn't find how to use spy to check that exportBlacklist is called only when '#blacklists_csv_export' is clicked.

도움이 되었습니까?

해결책

This is a very common scenario. Spy on the function, trigger the event handler, then check that the spy was called.

describe("Blacklist exporting", function () {
    it("exports on click", function () {
        spyOn(window, "exportBlacklists");
        $("#blacklists_csv_export").triggerHandler("click");
        expect(window.exportBlacklists).toHaveBeenCalled();
    });
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top