尝试为我的角度过滤器运行我的茉莉花单元测试。这就是过滤器的样子:
(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(')', '');
}
..
所以我在mymodule中定义了它,这就是单元测试的样子:
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>');
});
})
但是在运行测试时遇到此错误:
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.
更新:我实际上遇到主模块和从属模块的情况:
var app = angular.module('plunker', ['ngRoute','mymodule']);
var app2 = angular.module('mymodule');
改变了plunkr:
答案 0 :(得分:0)
我认为您错过了模块定义中的依赖关系数组。应该是:
var app = angular.module('mymodule', [])
我还将模块注入一个单独的forEach块。
试试这个
<强> 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
}
};
});
<强>规范强>
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>');
});
});
答案 1 :(得分:0)
感谢您将plunkr放在一起。这对我来说是实例化测试的。测试失败了,但你应该可以在这里工作:
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>');
});
});
希望这有帮助。