Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 在《茉莉花》中有没有一种模仿对象的较短方法?_Unit Testing_Jasmine_Karma Jasmine - Fatal编程技术网

Unit testing 在《茉莉花》中有没有一种模仿对象的较短方法?

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

我有一个角度过滤器函数,它依赖于$window函数,该函数有一个非常大的命名空间:

.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),从而使代码和模拟更容易。