Karma: how to mock global variable that is declared in html?
-
20-12-2019 - |
Question
I have a variable declared in html as
<script>
var actionUrl = '@Url.Action(action, "Designer")';
</script>
so that the variable actionUrl can be called in the javascript file I want to test. However, as the unit test isolates each file to test, the actionUrl becomes undefined in karma. How do I mock this variable? What is the best practice?
Solution
- As suggested, by calling
window.actionUrl = 'whatever';
- Or maybe it'd make sense to invent some structure for mock-objects
unit-tests
|
mocks -
| - module1.js
...
| - moduleN.js
| - globals.js
and then put all global objects in globals.js
and load it inside karma.config.js
Edit: added global mocks to the fork of Angular Seed https://github.com/sbedulin/angular-js-dev-guide
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow