Skip to Content
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
Explore Skills
Cloud Computing
Microsoft Azure
Amazon Web Services (AWS)
Google Cloud
Cloud Migration
Cloud Deployment
Cloud Platforms
Data Engineering
Data Warehouse
SQL
Apache Spark
Microsoft SQL Server
MySQL
Kafka
Data Lake
Streaming & Messaging
NoSQL Databases
Relational Databases
Data Science
Pandas
R
MATLAB
SAS
D3
Power BI
Tableau
Statistics
Exploratory Data Analysis
Data Visualization
AI & ML
Generative AI
Machine Learning
Artificial Intelligence (AI)
Deep Learning
Reinforcement Learning
Natural Language Processing
TensorFlow
Scikit-Learn
Hyperparameter Tuning
MLOps
Programming Languages
Java
JavaScript
Spring
Python
Go
C#
C++
C
Swift
Rust
Functional Programming
Software Architecture
Object-Oriented
Distributed Systems
Domain-Driven Design
Architectural Patterns
IT/Ops
Kubernetes
Docker
GitHub
Terraform
Continuous Delivery
Continuous Integration
Database Administration
Computer Networking
Operating Systems
IT Certifications
Security
Network Security
Application Security
Incident Response
Zero Trust Model
Disaster Recovery
Penetration Testing / Ethical Hacking
Governance
Malware
Security Architecture
Security Engineering
Security Certifications
Design
Web Design
Graphic Design
Interaction Design
Film & Video
User Experience (UX)
Design Process
Design Tools
Business
Agile
Project Management
Product Management
Marketing
Human Resources
Finance
Team Management
Business Strategy
Digital Transformation
Organizational Leadership
Soft Skills
Professional Communication
Emotional Intelligence
Presentation Skills
Innovation
Critical Thinking
Public Speaking
Collaboration
Personal Productivity
Confidence / Motivation
Features
All features
Verifiable skills
AI Academy
Courses
Certifications
Interactive learning
Live events
Superstreams
Answers
Insights reporting
Radar Blog
Buy Courses
Plans
Sign In
Try Now
O'Reilly Platform
book
React学习手册
by
Alex Banks
,
Eve Porcello
December 2017
Intermediate to advanced
329 pages
6h 9m
Chinese
China Electric Power Press Ltd.
Content preview from
React学习手册
242
|
第
10
章
const _click = jest.fn()
shallow(
<Star onClick={_click} />
)
.find('div.star')
.simulate('click')
expect(_click).toBeCalled()
})
在这个测试中的一个模拟函数
_click
,是通过
jest.fn
创建的。当我们渲染星标时,
将上述模拟函数作为
onClick
的属性传递给了它。接下来,对被渲染的
div
元素进行定
位,然后使用
Enzyme
的
simulate
方法在该元素上模拟了一个
click
事件。单击星标会
触发
onClick
属性,继而触发执行模拟函数。
.toBeCalled
匹配器可以用来验证该模拟
函数是否被触发了。
Enzyme
可以用来帮助用户渲染组件,查找被渲染的
DOM
元素或者其他组件,并且可
以和它们进行交互。
模拟组件
上一个测试引入了模拟的概念:我们使用一个模拟函数测试
Star
组件。
Jest
包含丰富的
工具,它们可以帮助用户创建和注入各种不同的模拟效果,以便用户可以写出更好的
测试代码。模拟对象是一种非常重要的测试技术,它可以帮助用户聚焦于单元测试。
模拟对象就是可以用来替换测试中真实对象的对象。
注
4
模拟对象对于测试的世界来说就像是好莱坞的动作替身演员。模拟对象和替身演员都
可以用来替代真身(一个组件或者影视明星)。在一部电影中,替身看上去和演员本
人没什么两样。在一个测试中,模拟对象可以被当作一个真实的对象。
模拟的目的是允许用户能够聚焦于被测试的组件或者对象,即 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial
You might also like
React快速上手开发
Stoyan Stefanov
流畅的Python
Luciano Ramalho
C++语言导学(原书第2版)
本贾尼 斯特劳斯特鲁普
C++程序设计:原理与实践(基础篇)(原书第2版)
本贾尼 斯特劳斯特鲁普
Publisher Resources
ISBN: 9787519814236