gpt4 book ai didi

backbone.js - 如何将 React 测试实用程序与 Jasmine 一起使用

转载 作者:行者123 更新时间:2023-12-03 13:37:51 25 4
gpt4 key购买 nike

我使用 React 的 test utils 制作了单元测试代码。但遇到了问题

我的环境是:

  • 轨道 4
  • Jasmine 2.0.0
  • 主干1.1.2
describe("cNotice", function () {
it("lol", function () {
console.log(Notice); // present
console.log(<Notice message="show me the message" />); // return Constructor

var instance = <Notice message="show me the message" />;
var component = React.addons.TestUtils.renderIntoDocument(instance);
expect(component.getDOMNode().childNodes[0].className).toBe('notice');
});
});

错误消息是:

Error: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. This usually means that you're trying to add a ref to a component that doesn't have an owner (that is, was not created inside of another component's render method). Try rendering this component inside of a new top-level component which will hold the ref.

<小时/>

更新

这段代码没有问题:

describe("cNotice", function () {
var Notice = null;
beforeEach(function () { Notice = React.createClass({...}); });

it("lol", function () {
var instance = <Notice message="show me the message" />;
var component = React.addons.TestUtils.renderIntoDocument(instance);
expect(component.getDOMNode().childNodes[0].className).toBe('notice');
});
});

但我想从外部文件导入通知组件。

最佳答案

已解决

我使用了窗口命名空间。
从外部文件导入通知组件

describe("cNotice", function () {
it("lol", function () {
var component = React.addons.TestUtils.renderIntoDocument(window.Notice({ message: "show me the message" }));
expect(component.getDOMNode().childNodes[0].className).toBe('notice');
});
});

关于backbone.js - 如何将 React 测试实用程序与 Jasmine 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22062663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com