Unit testing 在《茉莉花》中有没有一种模仿对象的较短方法?
我有一个角度过滤器函数,它依赖于$window函数,该函数有一个非常大的命名空间:Unit testing 在《茉莉花》中有没有一种模仿对象的较短方法?,unit-testing,jasmine,karma-jasmine,Unit Testing,Jasmine,Karma Jasmine,我有一个角度过滤器函数,它依赖于$window函数,该函数有一个非常大的命名空间: .filter('dbNonUtcToDate', function ($window) { //for fields like birthdate in EmployeeData which are not UTC return function (val, defaultVal) { if (val !== null) { var mdate = $windo
.filter('dbNonUtcToDate', function ($window) { //for fields like birthdate in EmployeeData which are not UTC
return function (val, defaultVal) {
if (val !== null) {
var mdate = $window.moment($.jsonDateToDate(val)).format($window.Fr.Alpha.Rep.Web.context.dateFormat().toUpperCase());
var nullMDate = $window.moment([1, 0, 1]).format($window.Fr.Alpha.Rep.Web.context.dateFormat().toUpperCase());
if (mdate !== nullMDate) {
return mdate;
}
}
return defaultVal !== undefined ? defaultVal : '';
};
})
有没有一种方法可以模拟$window.Fr.Alpha.Rep.Web.context.dateFormat()而不执行以下操作:
beforeEach(module(function ($provide) {
var fakeWindow = {
Fr:
{
Alpha:
{
Rep:
{
Web:
{
context:
{
dateFormat: function () {
return ...;
}
}
}
}
}
}
}
$provide.value('$window', fakeWindow);
}));
你能直接注入moment和dateFormat而不是做“火车失事”吗?我一直听说最好尽可能地隔离单元测试并尝试模拟第三方库。我的意思是,不要通过$window,而是将DI配置为通过(moment,dateFormat),从而使代码和模拟更容易。