javascript - How to unit test function having global variable using Chai Mocha and sinon? -


consider function,

    function upgradeplans (arr, curplan) {         var ispaidcustomer = cust && cust.type === "subscriber" && cust.subtype === "paid";         (var = 0, max = arr.length ; < max ; i++) {            if (ispaidcustomer) {               if (plans_index[arr[i].plansize] > curplan)) {                  arr[i].upgradeplan = true;               }               continue;            }            break;         }         return arr;     }, 

in function, cust , plans_index global object, test not have access these.

now, want unit test function e.g

  • it returns arr when cust not defined
  • it returns arr when cust not subscriber
  • it returns arr when cust not paid subscriber

to solve this, tried change upgradeplans function way,

   function upgradeplans (arr, curplan, cust, plans_index) {       // same content above function    }  

to test function having global variable, have pass variable argument.

this solves problem requires lot of changes in code.

is there other way test above function without changing in code ?


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -