我想爲我的過濾器編寫一個茉莉花測試。 這裏是我的過濾器:
angular.module('CPSCore.Filters').filter('TextToHtmlSafe', ['$sce', function ($sce)
{
return function (text)
{
if (!text)
return text;
var htmlText = text.replace(/\<br \/\>/g, '\n');
htmlText = htmlText.replace(/\<br\/\>/g, '\n');
htmlText = htmlText.replace(/\<br\>/g, '\n');
htmlText = htmlText.replace(/\</g, '< ');
htmlText = htmlText.replace(/\&/g, '& ');
htmlText = htmlText.replace(/\n/g, '<br />');
return $sce.trustAsHtml(htmlText);
};
}]);
這裏是我的茉莉花測試:
describe('CPSCore.Filters', function() {
var TextToHtmlSafeFilter, $sce;
beforeEach(module('CPSCore.Filters'));
beforeEach(inject(function (_$sce_, $filter) {
$sce = _$sce_;
TextToHtmlSafeFilter = $filter('TextToHtmlSafe');
}));
it('should replace \n with <br />', function() {
expect($sce.getTrustedHtml(TextToHtmlSafeFilter('testing\n'))).toEqual('testing<br />');
});
});
運行測試時,我在噶收到此錯誤:
錯誤:未知提供商:$ sceProvider < - $ sce
任何人都可以告訴我我做錯了什麼?
您是否正確創建模塊?你在測試中定義了'angular.module('CPSCore.Filters',[])'(括號)嗎? – Michael
模塊正常工作。測試失敗,所有的代碼都在上面我沒有遺漏任何東西 – Dritzz