说茉莉间谍没有被称为,但我可以看到它被称为

时间:2018-11-27 20:00:38

标签: jasmine es6-modules jasmine2.0

我无法弄清楚为什么Jasmine声称没有监视我正在监视的函数,尤其是因为调用时它正在登录buildLinksObj而不是在删除.and.callThrough()时调用感觉我之前写过很多类似的代码没有任何问题。我正在使用Jasmine 2.9

我收到的错误消息是:

1) addToLinks should call buildLinksObj if its given an object with children 
     it should add the personalized links to PageApp.meta.analytics.links
     Expected spy buildLinksObj to have been called.
    at UserContext.<anonymous> (http://localhost:9877webpack:///tests/specs/common/FetchPersonalContent.spec.js:854:0 <- tests/app-mcom.js:104553:48)

这是我的代码的例外:

FetchPersonalContent.js

const buildLinksObj = (responseObj = {}, targetObj, PageApp) => {
  console.log('it logs in buildLinksObj') // This is logging!
}

const addToLinks = (responseArr, personalizedLinks) => {
  responseArr.forEach((media) => {
    const type = media.type;
      const typeObj = media[type];
      buildLinksObj(typeObj, personalizedLinks, PageApp);
      if (typeObj && typeObj.children) {
        console.log('has children!')
        console.log('typeObj.children is: ', typeObj.children);
        typeObj.children.forEach((child) => {
          console.log('has a child')
          buildLinksObj(child, personalizedLinks, PageApp);
          console.log('buildLinksObj was definitely called. what the heck?')
        });
      }
    });
}

export {buildLinksObj, addToLinks, FetchPersonalContent as default,
};

FetchPersonalContent.spec.js

import * as FetchPersonalContent from '../../../src/FetchPersonalContent'; // my path is definitely correct

describe('it should add the personalized links to PageApp.meta.analytics.links', () => {
  it('addToLinks should call buildLinksObj if its given an object with children ', () => {
    spyOn(FetchPersonalContent, 'buildLinksObj').and.callThrough();
    FetchPersonalContent.addToLinks([{
    "personalId": 30718,
    "type": "carousel",
    "carousel": {}
  }], {});
    expect(FetchPersonalContent.buildLinksObj).toHaveBeenCalled();
  });
});

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我感觉规范文件中的FetchPersonalContent.buildLinksObj与FetchPersonalContent.js文件中的buildLinksObj指向的实例不同。

为什么需要export {FetchPersonalContent as default}?我假设您在问题中共享了FetchPersonalContent.js的完整内容。

可能的解决方案:

您可以尝试从FetchPersonalContent语句中删除export

代替

export {buildLinksObj, addToLinks, FetchPersonalContent as default,
};

您可以直接export文件中的常量FetchPersonalContent.js