Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.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
Reactjs 如何使用Jest测试连接到存储的React视图?_Reactjs_React Jsx_Reactjs Flux_Jestjs_Reactjs Testutils - Fatal编程技术网

Reactjs 如何使用Jest测试连接到存储的React视图?

Reactjs 如何使用Jest测试连接到存储的React视图?,reactjs,react-jsx,reactjs-flux,jestjs,reactjs-testutils,Reactjs,React Jsx,Reactjs Flux,Jestjs,Reactjs Testutils,我有一个反应视图,它与商店进行通信。我已经成功地分别测试了视图和存储,但没有组合测试 我遵循了命令,但收到一个类型错误。看起来Jest正在尝试将存储注册为组件,即使我使用了dontMock Using Jest CLI v0.2.0 PASS __tests__/unit/spec/stores/ViewStore-spec.js (0.248s) FAIL __tests__/unit/spec/components/View-spec.react.js (0.942s) undefi

我有一个反应视图,它与商店进行通信。我已经成功地分别测试了视图和存储,但没有组合测试

我遵循了命令,但收到一个类型错误。看起来Jest正在尝试将存储注册为组件,即使我使用了
dontMock

Using Jest CLI v0.2.0
 PASS  __tests__/unit/spec/stores/ViewStore-spec.js (0.248s)
 FAIL  __tests__/unit/spec/components/View-spec.react.js (0.942s)
undefined
● View › it defaults to zero unread
  - TypeError: /Users/matnorri/dev/projects/matnorri/web-client/src/app/scripts/components/View.react.js: /Users/matnorri/dev/projects/matnorri/web-client/src/app/scripts/stores/ViewStore.js: Cannot call method 'register' of undefined
        at /Users/matnorri/dev/projects/matnorri/web-client/src/app/scripts/stores/ViewStore.js:43:31
        at Object.runContentWithLocalBindings (/Users/matnorri/dev/projects/matnorri/web-client/node_modules/jest-cli/src/lib/utils.js:357:17)
        ...
我已经在下面包含了我认为相关的代码,但是如果必要的话可以提供完整的代码

查看笑话测试

jest.dontMock('../../../../src/app/scripts/components/View.react');
jest.dontMock('../../../../src/app/scripts/stores/ViewStore');

describe('View', function() {

  var React;
  var TestUtils;
  var ViewComponent;
  var View;

  beforeEach(function() {
    React = require('react/addons');
    TestUtils = React.addons.TestUtils;
    ViewComponent =
      // Tried both lines with same effect.
      // require('../../../../src/app/scripts/components/View.react.js');
      require('../../../../src/app/scripts/components/View.react');
    View = TestUtils.renderIntoDocument(<ViewComponent />);
  });

  it('defaults to zero unread', function() {
    View._toString = jest.genMockFunction();
    View._onChange();
    expect(View.state.unread).toBe(0);
  });
});
jest.dontMock('../../../../src/app/scripts/components/View.react');
jest.dontMock('../../../../src/app/scripts/stores/ViewStore');
描述('视图',函数(){
var反应;
var-TestUtils;
var视图组件;
var观点;
beforeach(函数(){
React=要求('React/addons');
TestUtils=React.addons.TestUtils;
视图组件=
//用同样的效果试了两行。
//需要('../../../../src/app/scripts/components/View.react.js');
需要('../../../../src/app/scripts/components/View.react');
View=TestUtils.renderIntoDocument();
});
它('默认为零未读',函数(){
View.\u toString=jest.genMockFunction();
视图。_onChange();
expect(View.state.unread).toBe(0);
});
});

谢谢大家!

回答您的问题

我也遇到了这个问题,而不是嘲笑对象分配为我解决了这个问题。添加
jest.dontMock('object-assign')到您的等级库文件。这将解决问题

// View Jest test
jest.dontMock('../../../../src/app/scripts/components/View.react');
jest.dontMock('../../../../src/app/scripts/stores/ViewStore');

模拟还是不模拟

有关何时“模拟或不模拟”的更多信息,请参阅

Jest修改“require”方法的默认行为并实现它自己的require方法。通过这种方式,他们能够模拟文件中的每个函数。因此,如果您想让某些东西真正起作用(比如商店),就不应该模拟该文件

在您的情况下,您没有模拟ViewStore,因此将调用此存储上的每个函数。因为您没有模拟
对象分配
对象分配功能无法工作。这样,ViewStore的创建方式不正确(它返回
undefined
),测试抛出错误
无法调用undefined
的方法“register”,因为
assign
函数不起作用,因为它被模拟了

再次;如果您通读Jest网站上的文档,您会发现关于何时“模拟或不模拟”的详细描述


额外信息


我建议将“对象分配”和其他常用模块(如lodash/下划线)添加到测试配置中的
unmockedModulePathPathPatterns
。更多信息:

感谢您的回答、解释和其他链接。我很感激。这件事快把我逼疯了!我知道这种感觉,在我发现之前,我也花了一些时间。很高兴我能帮上忙。@stefan,我也遇到了同样的问题,但将对象分配添加到我的未修改路径似乎没有任何作用。建议
“UnmockedModulePathPathPatterns”:[“/node_modules”,“object assign”]
正如@Stefan所说,您可以将以下内容添加到包中。json:
“jest”:{“UnmockedModulePathPathPatterns”:[“/node_modules”],}
现在您不需要为节点模块指定jest.dontMock(…)。有关更多jest配置选项,请参阅: