angularjs - How to use momentJs in Karma/Jasmine unit tests for AngularJS?
问题描述
I am using momentJS in my AngularJS project.
I am trying to write unit tests and I keep getting issues with moment.
Here is my config file:
// list of files / patterns to load in the browser
files: [
// './bower_components/moment/min/moment.min.js',
// './bower_components/moment-timezone/builds/moment-timezone.min.js',
// './bower_components/moment/min/moment-with-locales.js',
// './bower_components/moment-timezone/builds/moment-timezone-with-data.js',
'./bower_components/moment/moment.js',
'./bower_components/moment-timezone/moment-timezone.js',
'./source/builds/vendors.js',
'./node_modules/angular-mocks/angular-mocks.js',
'./source/builds/vendors.css',
'./source/builds/app.css',
'./source/builds/bundle.js',
'./source/js/**/*.spec.js'
],
I keep getting this error: TypeError: moment(...).year(...).week is not a function
I'm not sure how to fix the error.
解决方案
为了立即运行单元测试,我们首先需要检查的是将 moment.js 和 angular-moment.js 文件包含在配置文件中
files: [
'../bower_components/moment/moment.js',
'../bower_components/angular-moment/angular-moment.js',
]
之后在规范文件上
describe('Moment Tests', function() {
var _moment;
beforeEach(inject(function(moment) {
_moment: moment
}));
});
describe('Test for date by provided format', function (){
it('should return date by provided format', function() {
var expectedYear = _moment(new Date().year());
expect(2019).toEqual(expectedDate);
});
});
希望这会有所帮助。
推荐阅读
- c# - Input.GetkeyDown 在没有按钮按下的情况下返回 true
- javascript - 在 2 秒延迟后,页面加载上的 Fade In Div 不起作用?
- android - 排行榜消息:“嗯,Play 游戏出了点问题”
- php - 我在正确的地方定义了 KERNEL_CLASS 吗?
- unit-testing - 是否需要告诉 Perl 编译器不要优化带有忽略返回值的函数调用?
- azure-active-directory - Azure 客户端凭据授予誓言在 Graph Mail API 访问的混合设置中不起作用
- filtering - DAX SUMMARIZE 函数在我的 Power BI 模型中不尊重基于行的安全性
- c# - WPF 按钮绑定到属性 Convertback IMultiValueConverter 不更新值
- python - 在大型 HTML 文件 python 中使用漂亮的汤正则表达式
- python - 在 Python 的列表中搜索包含“<”和“>”的值