[翻譯練習] 對視圖控制器壓入導航棧進行測試

譯自:swiftandpainless.com/testing-pus…


上個月我寫的關于使用 Swift 進行測試驅動開發的書終于出版了,我會在本文和接下來的一些博文中介紹這本書撰寫過程中的一些心得和體會。

在本文中,我將會展示一種很好的用來測試一個視圖控制器是否因為某個事件而被壓入導航棧的方法。

假設我們在視圖控制器上有一個按鈕。當用戶點擊按鈕時,一個新的視圖控制器應該被壓入導航棧中。我們怎么測試這個呢?

簡單!我們可以通過對導航欄控制器進行 Mock 的方式來達到我們的目的,就像接下來這樣:

class MockNavigationController: UINavigationController {var pushedViewController: UIViewController?override func pushViewController(viewController: UIViewController, animated: Bool) {pushedViewController = viewControllersuper.pushViewController(viewController, animated: true)}
}
復制代碼

確切地說,這是部分 Mock。這里我們建立了一個 UINavigationController 的子類,并且它只重寫了父類的一個方法。這個 Mock 操作會在 pushViewController(_:animated:) 被調用時被觸發,并且會將傳入的視圖控制器也就是第一個參數保存下來。

測試代碼如下所示:

func testTappingPushButton_PushesDetailViewControllerOntoStack() {let viewController = ViewController()let navigationController = MockNavigationController(rootViewController: viewController)UIApplication.sharedApplication().keyWindow?.rootViewController = navigationControllerguard let view = viewController.view as? View else { XCTFail(); return }view.button.sendActionsForControlEvents(.TouchUpInside)XCTAssertTrue(navigationController.pushedViewController is DetailViewController)
}
復制代碼

首先,我們將一個視圖控制器設為我們 Mock 導航欄控制器實例的 rootViewController。然后我們將導航欄控制器設為 UIApplication 單例的 keyWindowrootViewController。為了能夠將一個視圖控制器壓入導航棧中,視圖控制器的視圖必須位于視圖層次里,這一步是必須的。

下一步,我們獲取按鈕并且向它發送 .TouchUpInside 事件。最后我們斷言彈出的視圖控制器是 DetailViewController 類型的。

你可以在 GitHub 上找到相應的示例代碼。

你可以在我的書中找到更多貼近實際的測試例子。當你讀這本書時,請別忘了告訴我你對這本書的看法。


如有任何知識產權、版權問題或理論錯誤,還請指正。
https://juejin.im/post/5a3217cd6fb9a045204c3ce5
轉載請注明原作者及以上信息。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391652.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391652.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391652.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python多人游戲服務器_Python在線多人游戲開發教程

python多人游戲服務器This Python online game tutorial from Tech with Tim will show you how to code a scaleable multiplayer game with python using sockets/networking and pygame. You will learn how to deploy your game so that people anywhere around the world …

版本號控制-GitHub

前面幾篇文章。我們介紹了Git的基本使用方法及Gitserver的搭建。本篇文章來學習一下怎樣使用GitHub。GitHub是開源的代碼庫以及版本號控制庫,是眼下使用網絡上使用最為廣泛的服務,GitHub能夠托管各種Git庫。首先我們須要注冊一個GitHub賬號,打…

leetcode132. 分割回文串 II(dp)

給你一個字符串 s,請你將 s 分割成一些子串,使每個子串都是回文。 返回符合要求的 最少分割次數 。 示例 1: 輸入:s “aab” 輸出:1 解釋:只需一次分割就可將 s 分割成 [“aa”,“b”] 這樣兩個回文子串…

數據標準化和離散化

在某些比較和評價的指標處理中經常需要去除數據的單位限制,將其轉化為無量綱的純數值,便于不同單位或量級的指標能夠進行比較和加權。因此需要通過一定的方法進行數據標準化,將數據按比例縮放,使之落入一個小的特定區間。 一、標準…

熊貓tv新功能介紹_熊貓簡單介紹

熊貓tv新功能介紹Out of all technologies that is introduced in Data Analysis, Pandas is one of the most popular and widely used library.在Data Analysis引入的所有技術中,P andas是最受歡迎和使用最廣泛的庫之一。 So what are we going to cover :那么我…

關于sublime-text-2的Package Control組件安裝方法,自動和手動

之前在自己的文章《Linux下安裝以及破解sublim-text-2編輯器》的文章中提到過關于sublime-text-2的Package Control組件安裝方法。 當時使用的是粘貼代碼: 1import urllib2,os;pfPackage Control.sublime-package;ippsublime.installed_packages_path();os.makedirs…

上海區塊鏈會議演講ppt_進行第一次會議演講的完整指南

上海區塊鏈會議演講pptConferences can be stressful even if you are not giving a talk. On the other hand, speaking can really boost your career, help you network, allow you to travel for (almost) free, and give back to others at the same time.即使您不講話…

windows下Call to undefined function curl_init() error問題

本地項目中使用到curl_init()時出現Call to undefined function curl_init()的錯誤,去掉php.ini中的extensionphp_curl.dll前的分號還是不行,phpinfo()中無curl模塊,于是上網搜索并實踐了如下方法,成功: 在使用php5的c…

數據轉換軟件_數據轉換

數據轉換軟件📈Python金融系列 (📈Python for finance series) Warning: There is no magical formula or Holy Grail here, though a new world might open the door for you.警告 :這里沒有神奇的配方或圣杯,盡管新世界可能為您…

leetcode 1047. 刪除字符串中的所有相鄰重復項(棧)

給出由小寫字母組成的字符串 S,重復項刪除操作會選擇兩個相鄰且相同的字母,并刪除它們。 在 S 上反復執行重復項刪除操作,直到無法繼續刪除。 在完成所有重復項刪除操作后返回最終的字符串。答案保證唯一。 示例: 輸入&#x…

spring boot: spring Aware的目的是為了讓Bean獲得Spring容器的服務

Spring Aware的目的是為了讓Bean獲得Spring容器的服務 //獲取容器中的bean名稱import org.springframework.beans.factory.BeanNameAware;//獲得資源加載器,可以獲得額外的資源import org.springframework.context.ResourceLoaderAware; package ch2.aware; import …

10張圖帶你深入理解Docker容器和鏡像

【編者的話】本文用圖文并茂的方式介紹了容器、鏡像的區別和Docker每個命令后面的技術細節,能夠很好的幫助讀者深入理解Docker。這篇文章希望能夠幫助讀者深入理解Docker的命令,還有容器(container)和鏡像(image&#…

matlab界area_Matlab的數據科學界

matlab界area意見 (Opinion) My personal interest in Data Science spans back to 2011. I was learning more about Economies and wanted to experiment with some of the ‘classic’ theories and whilst many of them held ground, at a micro level, many were also pur…

javascript異步_JavaScript異步并在循環中等待

javascript異步Basic async and await is simple. Things get a bit more complicated when you try to use await in loops.基本的async和await很簡單。 當您嘗試在循環中使用await時,事情會變得更加復雜。 In this article, I want to share some gotchas to wat…

白盒測試目錄導航

白盒測試目錄導航(更新中) 2017-12-29 [1] 白盒測試:為什么要做白盒測試 [2] 白盒測試:理論基礎 [3] 白盒測試實戰(上) [4] 白盒測試實戰(中) [5] 白盒測試實戰(下&#…

hdf5文件和csv的區別_使用HDF5文件并創建CSV文件

hdf5文件和csv的區別In my last article, I discussed the steps to download NASA data from GES DISC. The data files downloaded are in the HDF5 format. HDF5 is a file format, a technology, that enables the management of very large data collections. Thus, it is…

CSS仿藝龍首頁鼠標移入圖片放大

CSS仿藝龍首頁鼠標移入圖片放大&#xff0c;效果似乎沒有js好。。。。。。 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>圖片放大</title><style>*{padding:0;margin:0;}body{padding-…

leetcode 224. 基本計算器(棧)

給你一個字符串表達式 s &#xff0c;請你實現一個基本計算器來計算并返回它的值。 示例 1&#xff1a; 輸入&#xff1a;s “1 1” 輸出&#xff1a;2 示例 2&#xff1a; 輸入&#xff1a;s " 2-1 2 " 輸出&#xff1a;3 示例 3&#xff1a; 輸入&#xff…

機械制圖國家標準的繪圖模板_如何使用p5js構建繪圖應用

機械制圖國家標準的繪圖模板The theme for week #5 of the Weekly Coding Challenge is:每周編碼挑戰第5周的主題是&#xff1a; 創建繪圖應用程序 (Creating a Drawing Application) This is the first application that we are building in the #weeklyCodingChallenge prog…

機器學習常用模型:決策樹_fairmodels:讓我們與有偏見的機器學習模型作斗爭

機器學習常用模型:決策樹TL; DR (TL;DR) The R Package fairmodels facilitates bias detection through model visualizations. It implements a few mitigation strategies that could reduce bias. It enables easy to use checks for fairness metrics and comparison betw…