第八章 Core Data by tutorials 笔记(六).pdfVIP

  • 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() {

文档评论(0)

1亿VIP精品文档

相关文档