【CodeTest】Mocking in Swift

学习资料

Mocking in Swift

在学习Swift单元测试的过程中,发现QuickSleipnir均没有提供mock的解决方案,感觉很诧异,难道Swift还这么不成熟么?经过查阅资料,才发现,原来由于Swift的一些新的语言特性(在函数或方法中,可以声明实现类),导致专门为Swift写一套第三方的mock框架已经不再必要.

我们仿照行为驱动开发举例说明中的第一个例子,消息格式化EventDescriptionFormatter写一个测试.

源码:

EventProtocol.swift

import Foundation

public protocol EventProtocol {

   var name     : String {get set}
   var startDate: NSDate {get set}
   var endDate  : NSDate {get set}
    
    
}  

EventDescriptionFormatter.swift

import Foundation

public extension NSDate {

    class func dateFromeString(dateString: String) -> NSDate {
    
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        return dateFormatter.dateFromString(dateString)!
    }
    
    func descriptionString() -> String {
    
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        return dateFormatter.stringFromDate(self)
    }
}

public class EventDescriptionFormatter {

    public init() {
    
    }
    
    public func eventDescriptionFromEvent(event : EventProtocol) -> String {
    
        return "\(event.name):开始于\(event.startDate.descriptionString()),结束于\(event.endDate.descriptionString())"
    }
}  

EventDescriptionFormatterSpec.swift

import Quick
import Nimble
import UseQuick

class EventDescriptionFormatterSpec: QuickSpec {
    
    override func spec() {
        
        class MockEvent: EventProtocol {
        
            var name      : String
            var startDate : NSDate
            var endDate   : NSDate
            
            init() {
            
                self.name      = "Fixture Time"
                self.startDate = NSDate.dateFromeString("2015-11-27 09:52:00")
                self.endDate   = NSDate.dateFromeString("2015-11-27 10:52:00")
            }
            
        }
        
        describe("EventDescriptionFormatter") { () -> Void in
            
            var descriptionFormatter : EventDescriptionFormatter!
            let mockEvent = MockEvent()
            
            beforeEach { () -> () in
                
                descriptionFormatter = EventDescriptionFormatter()
            }
            
            context(".eventDescriptionFromEvent") { () -> Void in
                
                it("should describe the event", closure: { () -> () in
                    
                    expect(descriptionFormatter.eventDescriptionFromEvent(mockEvent)).to(equal("Fixture Time:开始于2015-11-27 09:52:00,结束于2015-11-27 10:52:00"))
                })
            }
        }
    }
}  

以上使用了Quick.

下载源码

下载地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目录Swift学习资料@完整App@App框架@ 响应式框架@ UI@ 日历三方库@下拉刷新@模糊效果@富文本@图...
    IOS开发攻城狮_Fyc阅读 11,422评论 1 90
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,190评论 4 61
  • 今天几件事 总算解决了三年级两个班级缺数学老师的难题,同时二年级一个班缺数学老师的问题也得了很好的处理,不过二年级...
    甲午之印阅读 1,189评论 0 0
  • 最近热播戏《欢乐颂》吸引了无数人的眼球,这主要是五个女人的戏码,每个角色都有现实生活中人们的缩影在,而让我印象深刻...
    花开candy阅读 7,600评论 24 44
  • 最后一周,还有3天 农历2014年,还有7天。 这次,不管公历或是农历,2014真的再也不可能出现在我们的世界里了...
    ambire阅读 1,391评论 1 1