在玩笑测试中无法通过覆盖键从dict中获取元素

时间:2018-10-12 09:44:47

标签: javascript testing mocking jestjs

my_issue.js

const SPEAKERS = {
    'en-us': 'Amy',
    'de-de': 'Vicki',
    'fr-fr': 'Mathieu',
    'ru-ru': 'oksana'
};

let language_code = 'ru-ru';
let speed = '0.0';

let data = {};

function do_it() {
    data.a = 'good';
    data.e = language_code in SPEAKERS;
    data.b = SPEAKERS[language_code];
    data.c = speed;
    data.d = language_code;
}

do_it();

my_issue.test.js

let testModule = require('../my_issue');

let do_it = testModule.__get__('do_it');


describe('do_it', () => {
    it('do_it', () => {
        testModule.__set__('language_code', 'en-us');
        testModule.__set__('speed', '1.0');

        do_it();
        let language_code = testModule.__get__('language_code');
        expect(language_code).toEqual('en-us');

        let data = testModule.__get__('data');
        expect(data.a).toEqual('good');
        expect(data.c).toEqual('1.0');  // overwritten success
        expect(data.d).toEqual('en-us');  // overwritten success
        expect(data.e).toEqual(true);  // key exists in dict
        expect(data.b).toEqual('Amy');  // <<-- oksana SPEAKERS[language_code]
    });
});

您好,我有问题,当我使用ovveride language_code时,无法使用现有密钥从SPEAKERS获取元素。如您所见,“速度”和“语言代码”已成功覆盖。密钥存在于SPEAKER中,但无法通过此密钥获取价值。我做错了什么?谢谢大家。

0 个答案:

没有答案