美文网首页iOS - TDD / BDD iOS
【CodeTest】Mocking in Swift

【CodeTest】Mocking in Swift

作者: 刘大帅 | 来源:发表于2015-11-30 23:30 被阅读165次

    学习资料

    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.

    下载源码

    下载地址

    相关文章

      网友评论

        本文标题:【CodeTest】Mocking in Swift

        本文链接:https://www.haomeiwen.com/subject/zuyjhttx.html