简体   繁体   中英

How to instantiate module for unit test?

Trying to run my jasmine unit test for my angular filter. This is what the filter looks like:

(function() {
  'use strict';
  var app = angular.module('mymodule');
      app.filter('customCurrency', function($filter) {
        return function(amount, currencySymbol, fractionSize) {
          var currency = $filter('currency');

          if (amount < 0) {
            return currency(amount, currencySymbol).replace('(', '-').replace(')', '');
          }
..

So I have defined it in mymodule and this is what the unit test looks like:

describe('custom currency Filter', function() {
  var myUpperFilter, $filter;

  beforeEach(function() {
    module('mymodule');
    inject(function($injector) {
      // Append Filter to the filter name

      // Usign $filter Provider
      $filter = $injector.get('$filter');
    });
  });

  it('if I have 2 zeros in decimals only display max of 2 zeros in decimals', function() {
    // using $filter
    expect($filter('customCurrency')(1.0011, '$', undefined)).toEqual('$1<span class="decimals">.00</span>');
  });

})

However getting this error when running the test:

Error: [$injector:modulerr] Failed to instantiate module mymodule due to:
Error: [$injector:nomod] Module 'mymodule' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

Update: I actually have a situation with a main module and a dependent one:

var app = angular.module('plunker', ['ngRoute','mymodule']);
var app2 = angular.module('mymodule');

changed plunkr:

http://plnkr.co/edit/Rcu5gojIJG1GSZEhU4sZ?p=preview

I think you've missed the dependency array in your module definition. Should be:

var app = angular.module('mymodule', [])

I also inject the module in a separate forEach block.

Try this

DEMO

app.js

var app = angular.module('mymodule', []);


app.filter('customCurrency', function($filter) {

    return function(amount, currencySymbol, fractionSize) {
      var currency = $filter('currency');

      if (amount < 0) {
        return currency(amount, currencySymbol).replace('(', '-').replace(')', '');
      }

      var rounded = round(amount, fractionSize),
        currencyString = currency(rounded, currencySymbol, fractionSize),
        amounts = currencyString.split("."),

        integerPart = amounts[0],
        fractionalPart = amounts[1] || false,
        zerosFromRight = countZerosFromRight(fractionalPart);


      if (fractionalPart && zerosFromRight > 2) {

        var lastNonZero = indexLastNonZero(fractionalPart);

        // only zeros in fractionalPart
        if (lastNonZero > -1) {
          return integerPart +
            '<span class="decimals">.' +
            fractionalPart.slice(0, lastNonZero + 1) + '</span>';
        }

        return integerPart + '<span class="decimals">.00</span>';

      }


      if (fractionalPart) {
        return integerPart + '<span class="decimals">.' + fractionalPart + '</span>';
      }

      return integerPart;
      /////////////////////////////////////////////

      function round(str, decimals) {
        var num = +str;

        return num.toFixed(decimals);
      }

      function indexLastNonZero(str) {
        var len = str.length;

        while (len--) {

          if (str[len] !== '0') {
            return len;
          }
        }

        return -1;
      }

      function countZerosFromRight(str) {

        var len = str.length,
          count = 0;

        while (len--) {

          if (str[len] === '0') {
            count++;
            continue;
          }

          break;
        }

        return count
      }

    };
  });

spec

describe('custom currency Filter', function() {

  var $scope = null;
  var ctrl = null;

  //you need to indicate your module in a test
  beforeEach(module('mymodule'));

  beforeEach(function() {
    inject(function($injector) {
      $filter = $injector.get('$filter');
    });
  });

  it('if I have 2 zeros in decimals only display max of 2 zeros in decimals', function() {
    // using $filter
    expect($filter('customCurrency')(1.0011, '$', undefined)).toEqual('$1<span class="decimals">.00</span>');
  });


});

Thanks for putting together the plunkr. This worked for me to instantiate the test. The test failed, but you should be able to work from here:

describe('custom currency Filter', function() {
  var myUpperFilter, $filter;

  beforeEach(module('mymodule'));

  beforeEach(inject(function($injector) {
    // Append Filter to the filter name

    // Usign $filter Provider
    $filter = $injector.get('$filter');
  }));

  it('if I have 2 zeros in decimals only display max of 2 zeros in decimals', function() {
    // using $filter
    expect($filter('customCurrency')(1.0011, '$', undefined)).toEqual('$1<span class="decimals">.00</span>');
  });
});

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM