9blue
9blue

Reputation: 4753

Karma: how to mock global variable that is declared in html?

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?

Upvotes: 4

Views: 10240

Answers (1)

sbedulin
sbedulin

Reputation: 4342

  1. As suggested, by calling window.actionUrl = 'whatever';
  2. 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

Upvotes: 6

Related Questions