2016-07-24 35 views
2

我正在根據Model-View-Controller模式在Swift中構建一個簡單的iOS應用程序。我可以通過給它輸入數據並根據我的期望斷言輸出結果來測試Model類。但我想知道如何測試Controller類?如何在MVC中測試控制器類?

看來,如果我想測試Controller類,測試邏輯將會複雜得多。有沒有測試Controller類的標準方法?

+2

控制器只是您的模型和您的用戶界面之間的粘合劑。給它的輸入數據就好像它是由模型給出的一樣,然後測試輸出到UI的輸出數據。 – Alexander

+1

https://cocoapods.org/pods/FBSnapshotTestCase是測試佈局和https://的好工具。 cocoapods.org/pods/KIF可用於通過劫持可訪問性功能來自動化觸摸事件(並將其與Swift橋接有一些缺陷)。儘管如此,他們都需要一些先決條件來進行測試。我建議如果你還沒有學習Quick&Nimble作爲單元測試工具 – markedwardmurray

+2

這個問題太廣泛了,可能主要是基於觀點的。 – JAL

回答

5

請勿測試您的UIViewController s。發生在他們身上的很多事情你沒有看到和/或無法控制。相反,在其他對象(如查看模型)中保留儘可能多的邏輯,而不是您的UIViewController。然後,您可以測試您的視圖模型,就像測試模型一樣。

編輯:

你會怎樣想構建你的UIViewController S和測試模型&視圖模型:

從這個代碼的主要外賣是:

  1. 使用Dependency Injection
  2. 在Release中給類賦予真正的依賴關係,並在測試中僞造依賴項

視圖控制器

// this class is super simple, so there's hardly any reason to test it now. 
class SomeViewController: UIViewController { 
    @IBOutlet weak var someLabel: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // we give the *real* user accessor in the view controller 
     let viewModel = SomeViewModel(userAccessor: DBUserAccessor()) 
     someLabel.text = viewModel.welcomeMessage 
    } 
} 

用戶模型

struct User { 
    var name: String 
} 

用戶訪問器

這是一些依賴你的視圖模型的需求。在發佈期間(在您的View Controller中)給出一個真實的。在測試過程中給出一個假的,以便你可以控制它。

protocol UserAccessor { 
    var currentUser: User? { get } 
} 

// since we're using the fake version of this class to test the view model, you might want to test this class on its own 
// you would do that using the same principles that I've shown (dependency injection). 
class DBUserAccessor: UserAccessor { 
    var currentUser: User? { 
     // some real implementation that's used in your app 
     // get the user from the DB 
     return User(name: "Samantha") // so not really this line, but something from CoreData, Realm, etc. 
    } 
} 

class FakeUserAccessor: UserAccessor { 
    // some fake implementation that's used in your tests 
    // set it to whatever you want your tests to "see" as the current User from the "DB" 
    var currentUser: User? 
} 

視圖模型

這是實際生活的邏輯,你想測試。

class SomeViewModel { 
    let userAccessor: UserAccessor 

    init(userAccessor: UserAccessor) { 
     self.userAccessor = userAccessor 
    } 

    var welcomeMessage: String { 
     if let username = self.username { 
      return "Welcome back, \(username)" 
     } else { 
      return "Hello there!" 
     } 
    } 

    var username: String? { 
     return userAccessor.currentUser?.name 
    } 
} 

測試

最後,你想怎麼對它進行測試。

class SomeViewModelTest: XCTestCase { 
    func testWelcomeMessageWhenNotLoggedIn() { 
     let userAccessor = FakeUserAccessor() 
     let viewModel = SomeViewModel(userAccessor: userAccessor) // we give the *fake* user accessor to the view model in tests 
     userAccessor.currentUser = nil // set the fake UserAccessor to not have a user "logged in" 

     // assert that the view model, which uses whatever you gave it, gives the correct message 
     XCTAssertEqual(viewModel.welcomeMessage, "Hello there!") 
    } 

    func testWelcomeMessageWhenLoggedIn() { 
     let userAccessor = FakeUserAccessor() 
     let viewModel = SomeViewModel(userAccessor: userAccessor) 
     userAccessor.currentUser = User(name: "Joe") // this time, the use is "logged in" 

     XCTAssertEqual(viewModel.welcomeMessage, "Welcome back, Joe") // and we get the correct message 
    } 
} 
+0

非常感謝你的建議!我想知道你是否可以詳細闡述「你可以測試你的視圖模型」。它只對測試模型有什麼區別和相似之處?再次感謝你的幫助! – Thor

+1

我的榮幸。我添加了一堆示例代碼,顯示瞭如何設置它。 – solidcell