提问人:Bhagya Swamy 提问时间:4/2/2019 最后编辑:Bhagya Swamy 更新时间:4/2/2019 访问量:3577
使用 Jasmine 对过滤器组件进行单元测试
Unit testing a filter component using Jasmine
问:
我创建了一个组件,用于过滤掉传递给它的字符串。我正在尝试为它编写单元测试,但它向我抛出了一个与构造函数相关的错误。请帮帮我。
import angular from 'angular';
import filter from "./format-string.filter"
angular
.module( "filters.truncateString", [] )
.filter( "truncateString", () => filter );
class TruncateString {
filter(val){
const accValue = val.split('YYYY');
const firstValue= accValue[1].replace(/^(|-?\d+)$/, '');
return `${accValue[0]} ${firstValue} AAA`;
}
}
export default TruncateString .filter;`
我写的测试是
import angular from 'angular';
import 'angular-mocks';
import TruncateString from './format-string.filter';
describe('TruncateString filter', () => {
let controller, filter;
beforeEach(angular.mock.module('filters.TruncateString'));
controller = new TruncateString();
beforeEach(inject(function(_$filter_){
filter = _$filter_;
}));
it('Checking whether the function is returning formatted string', () => {
const result = controller.filter('123YYY3213123');
expect(result).toHaveBeenCalledWith('123 YYY ... 23');
});
});
答:
1赞
Majesty
4/2/2019
#1
以下是测试过滤器的方法
describe('Test My Filter', () => {
let $filter;
beforeEach(() => {
module('filters.TruncateString');
inject((_$filter_) => {
$filter = _$filter_;
});
});
it('should test filter behavior', () => {
let result = $filter('truncateString')('testval');
expect(result).toEqual('expectedval');
});
});
评论
0赞
Bhagya Swamy
4/2/2019
成功了!非常感谢。我不得不将模块更改为angular.mock.module。
评论