在Angular App中使用ES6时,'这个'在茉莉花单元测试中的范围

时间:2016-03-24 21:05:03

标签: javascript angularjs unit-testing jasmine

我正在测试一个角度服务,它正在公开一个依赖于它所调用的上下文的方法。以下是我的服务 -

 'use strict';
class Utils {
    constructor($state) {
        this.$state = $state;
    }

    /**
     * Establish operation and perform a get if it is update
     * @param getCallback {function}
     * @param context {object}
     * @returns {string}
     */
    calculateCreateUpdateOpBasedOnState(getCallback, context) {
        var operation = this.$state.current.data.operation;
        if (operation === 'update') {
            getCallback.call(context);
        }
        return operation;
    }

    /**
     * Resolve the path to a value and return it
     * @param {Object} obj The object to lookup
     * @param {String} path dot separated path for value sought after
     * @param {*} [defaultValue=undefined] defaultValue value to return if the path is not found
     * @param {Function|String} [composeParam=undefined] composeFunction or member name used to generate the result based on the value found
     * WARNING: the conposeFunction is *NOT* called with the defaultValue
     * @return {*}
     */
    valueOf(obj, path, defaultValue, composeParam) {
        var value;

        if (path.split('.').every(function (member) {
                var memberValue = obj[member];
                if (undefined !== memberValue && null !== memberValue) {
                    obj = memberValue;
                    return true;
                }
                return false; // Stop the loop
            })) {
            if (undefined !== composeParam) {
                if ('function' === typeof composeParam) {
                    value = composeParam(obj);
                } else {
                    value = obj.valueOf(composeParam.toString(), defaultValue);
                }
            } else {
                value = obj;
            }
        } else {
            value = defaultValue;
        }
        return value;
    }

    /**
     * sets model according to path given(model) and returns model value
     * @param value {*}
     * @param model {string}
     * @returns {*}
     */
    modelGetterSetter(value, model) {

        if (value !== undefined && model !== undefined) {
            model = model.split('.').reverse();
            var current = this;
            while (model.length - 1) {
                if (typeof current !== 'object') return undefined;
                current = current[model.pop()];
            }
            current[model.pop()] = value;
        }
        else if (model !== undefined) {
            value = this.Utils.valueOf(this, model);
        }
        else {
            throw new Error('Model is undefined')
        }

        return value;
    }


    /*@ngInject*/
    static factory($state) {
        /* jshint ignore:start */
        'ngInject';
        /* jshint ignore:end */
        // Singleton
        Utils.instance = Utils.instance || new Utils($state);
        return Utils.instance;
    }
}

export default Utils;

我试图测试else if if modelGetterSetter方法,这是我最初的尝试 -

describe('modelGetterSetter Method without a value', function () {
    this.obj = {
        foo: {
            bar: 'val'
        }
    };
    let val,
        that = this;
    that.Utils = utils;
    beforeEach(()=> {
        "use strict";
        val = utils.modelGetterSetter.call(that, undefined, 'obj.foo.bar');
    });
    it('should return correct value when supplied with valid model and undefine value', ()=> {
        expect(val).toBe('val');
    });

});

然而,这导致以下错误 -

  

TypeError:'undefined'不是对象(评估'this.Utils.valueOf')

我设法通过声明在beforeEach中分配Utils服务来解决这个问题 -

describe('modelGetterSetter Method without a value', function () {
    this.obj = {
        foo: {
            bar: 'val'
        }
    };
    let val,
        that = this;

    beforeEach(()=> {
        "use strict";
        that.Utils = utils;
        val = utils.modelGetterSetter.call(that, undefined, 'obj.foo.bar');
    });
    it('should return correct value when supplied with valid model and undefine value', ()=> {
        expect(val).toBe('val');
    });

});

只是想知道为什么在beforeEach中完成任务但在之前没有这样做。即使我在两种情况下都进行了分配后调用了服务方法。

0 个答案:

没有答案