简体   繁体   English

类型错误:使用 jasmine 进行单元测试时未定义不是对象

[英]TypeError: undefined is not an object when unit testing with jasmine

I'm trying to write a unit test for a function and I'm getting an error.我正在尝试为函数编写单元测试,但出现错误。 I'm also unsure how to test other parts of the function correctly.我也不确定如何正确测试函数的其他部分。

private dictionaryMap (loggedIn, response) {
    const translations = this.convertToArrays(response.data.translations);

    this.configureMomentLocale(language);

    if (!loggedIn) {
        this.cachePublicDictionary(translations);
    }

    // not testing this part
    this.dictionary = new Dictionary({
        translationMap: Object.assign({}, this.getPublicDictionaryFromCache() || {}, translations),
    });

    return this.rx.Observable.of(this.dictionary);
}

And my unit test so far looks like this:到目前为止,我的单元测试如下所示:

describe('dictionaryMap', () => {

    it('calls configureMomentLocale()', () => {
        const foo = {
            'foo':'bar',
        };
        spyOn(service, 'configureMomentLocale');
        service.dictionaryMap({}, false);
        expect(service.configureMomentLocale).toHaveBeenCalled();
    });

});

And when I run this test I get this error:当我运行此测试时,出现此错误:

TypeError: undefined is not an object (evaluating 'response.data.translationMap')类型错误:未定义不是对象(评估“response.data.translationMap”)

Do I need to mock response.data.translations or assign the json structure?我是否需要模拟 response.data.translations 或分配 json 结构? (translationMap: {'email': 'email', 'forgotPassword': 'Forgot password?'}) (translationMap: {'email': 'email', 'forgotPassword': '忘记密码?'})

Also, I'm not sure how to properly test the other parts of the function, like the if statement or returning the observable.另外,我不确定如何正确测试函数的其他部分,例如 if 语句或返回 observable。 I am new to unit testing.我是单元测试的新手。

Your method dictionaryMap accepts 2 parameters - 1st is loggedIn (presumably boolean) and the 2nd one is response .您的方法dictionaryMap接受 2 个参数 - 第一个是loggedIn (大概是布尔值),第二个是response On the first line of that method (before calling configureMomentLocale ) you have a line const translations = this.convertToArrays(response.data.translations);在该方法的第一行(在调用configureMomentLocale之前)你有一行const translations = this.convertToArrays(response.data.translations); which expects the response variable to have a property named data .它期望response变量具有名为data的属性。

In your test, you have 2 errors on the line service.dictionaryMap({}, false);在您的测试中,您在service.dictionaryMap({}, false);行上有 2 个错误service.dictionaryMap({}, false); :

  1. You're setting the arguments in reverse order - you should put the boolean argument first and the object one second您以相反的顺序设置参数 - 您应该首先放置布尔参数,然后再放置对象
  2. The object doesn't have a property named data该对象没有名为data的属性

The line should be corrected to be something similar to service.dictionaryMap(false, { data: {} });该行应该更正为类似于service.dictionaryMap(false, { data: {} }); . . You might even need to define translations property for data object - it really depends on what this.convertToArrays function does and how it handles undefined values.您甚至可能需要为data对象定义translations属性 - 这实际上取决于this.convertToArrays函数的作用以及它如何处理undefined值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM