lundi 19 septembre 2016

Mocking an object for unit testing in Jasmine without using Require

I am trying to write specs for a JavaScript file (say 'functionality.js'). jQuery is the only external library used. I am using Karma as my test runner.

functionality.js in-turn refers to an object (say 'FOO') which is defined in a different file on which several methods are defined. The object FOO (and its methods) is used in several other files. I don't want to add in foo.js (where FOO is defined) to the list of files in Karma because that in turn makes use an object defined elsewhere and so on. I would like to be able to test functionality.js and others in isolation by mocking FOO as an empty object and be able to commonly use it in all my spec files. Would I be able to do that? Are there any other alternate patterns to this?

My trials: I tried creating a helper file and defined an empty FOO object wrapped in an IIFE, then added that file to Karma before I loaded my source JavaScript files, but it throws a ReferenceError saying can't find variable FOO in functionality.js:1

Aucun commentaire:

Enregistrer un commentaire