- 7
- 0
- 约7千字
- 约 5页
- 2017-06-07 发布于湖北
- 举报
Core Data by tutorials 笔记 (六)
原⽂出处: http://chengway .in/post/ji-zhu/core-data-by-tutorials-bi-ji-liu
今天我们来关注⼀下CoreData的单元测试,其实在写程序之前,先写测试,将问题⼀
分解,也是TDD所倡导的做法,这也是我今年所期望达成的⼀个⽬标,新开项⽬
按TDD的流程来做,以后也会整理些这⽅⾯的东西。如果你对CoreData的其他⽅⾯感
兴趣请查看我之前的笔记或直接购买 《Core Data by Tutorials》
Chapter 8: Unit Testing
作者列举了⼀系列单元测试的好处:帮助你在⼀开始就组织好项⽬的结构,可以不⽤
操⼼UI去测试核⼼功能。单元测试还可以⽅便重构。还可以更好地拆分UI进⾏测试。
这章主要焦距在XCTest这个框架来测试Core Data程序,多数情况下Core Data的测试要
依赖于真实的Core Data stac ,但又不想将单元测试的test data与你⼿动添加的接⼜测
试弄混,本章也提供了解决⽅案。
⼀、Getting started
本章要测试的是⼀个关于野营管理的APP ,主要管理营地、预订 (包括时间表和付款
情况)。作者将整个业务流程分解为三块:
1. campsites (野营地)
2. campers (野营者)
3. reservations (预订)
由于swift 内部的访问控制,app和其test分别属于不同的targets和不同的modules ,因此
你并不能普通地从tests中访问app 中的classes ,这⾥有两个解决办法:
1. 把App 中的classes和methods标记为public ,是其对tests可见。
2. 直接在test target⾥添加所需要的classes 。
作者提供的实例中,已经将要测试的类和⽅法标记为p ublic的了,现在就可以对Core
Data部分进⾏测试了,作者在测试开始前给了⼀些建议:
Good unit tests follow the acronym FIRST:
• Fast: If your tests ta e too long to run , you won’t bother running them .
• Isolated: Any test should function properly when run on its own or before or
after any other test .
• Repeatable: You should get the same results every time you run the test
against the same codebase .
• Self-verifying: The test itself should report success or failure; you shouldn’t
have to chec the contents of a file or a console log .
• Timely: There’s some benefit to writing the tests after you ’ve already written
the code , particularly if you ’re writing a new test to cover a new bug . Ideally ,
though , the tests come first to act as a specification for the functionality you ’re
developing .
为了达到上⾯提到“FIRST” ⽬标,我们需要修改Core Data stac 使⽤in-memory store⽽
不是SQLite-bac ed store 。具体的做法是为test target创建⼀个CoreDataStac 的⼦类来
修改store typ e 。
class TestCoreDataStack: CoreDataStack {
override init() {
您可能关注的文档
最近下载
- 贵州省六校联考2025届高三下学期3月高考实用性联考(四)英语试卷(含答案).docx VIP
- 颈椎病的中医护理课件.pptx VIP
- 原调正谱G小小蝴蝶la farfalletta钢琴伴奏高清打印版.pdf VIP
- 上海大学2024-2025学年第1学期《高等数学(上)》期末考试试卷(B卷)附参考答案.pdf
- 广发证券-交通银行-601328-零售转型提速,定增落地夯实资本根基.pdf
- 上海大学2024-2025学年第1学期《高等数学(上)》期末考试试卷(A卷)附参考答案.pdf
- 循环系统护理小讲课.pptx
- 建筑屋面西瓦专项施工方案(范本).doc VIP
- 2024年山东铝业职业学院单招综合素质考试题库及答案1套.docx VIP
- 探究TIPS术后支架堵塞的影响因素及抗凝治疗的必要性:基于多维度分析与临床实践.docx
原创力文档

文档评论(0)