简体   繁体   English

如何测试没有渲染方法的React Native类?

[英]How to test React Native classes that do not have a render method?

I am building a mobile application with React Native and ran into a problem when trying to unit test my code. 我正在用React Native构建一个移动应用程序,并且在尝试对我的代码进行单元测试时遇到了一个问题。 Currently I do have both Jest and Enzyme being used in the testing. 目前,我确实在测试中同时使用了Jest和Enzyme。

How would I be able to test the methods in a certain class/file that does not contain a render() method and does not "extend as a Component"? 如何在不包含render()方法并且不“扩展为组件”的特定类/文件中测试方法? This class is used to query information from an API and saving it into variables. 此类用于从API查询信息并将其保存到变量中。

The documentation about Jest and Enzyme at 有关Jest和Enzyme的文档,网址为

https://facebook.github.io/jest/docs/api.html#content https://facebook.github.io/jest/docs/api.html#content

http://airbnb.io/enzyme/docs/api/index.html http://airbnb.io/enzyme/docs/api/index.html

seem to be focused on testing rendered components. 似乎专注于测试渲染的组件。

Assuming you've got a module export set up, you actually just import the object/class and then write a test against it using a jasmine syntax. 假设您已经设置了模块导出,那么实际上您只需导入对象/类,然后使用茉莉花语法对它进行测试。 You can try it out live right here: 您可以在这里现场试用:

https://facebook.github.io/jest/docs/getting-started.html#content https://facebook.github.io/jest/docs/getting-started.html#content

If you notice on the "add-test.js" file, the first line is a require to get your object under test. 如果您注意到“ add-test.js”文件,则第一行是对您的对象进行测试的要求。 In this case: 在这种情况下:

const add = require('./add');

Then it's just a matter of regular old jasmine-style testing. 然后,这只是常规的旧茉莉花式测试的问题。

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

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