簡體   English   中英

Android Espresso測試中的存根/模擬意圖

[英]Stubbing/mocking intent in Android Espresso test

我想在我的應用程序中測試以下流程:

  1. 用戶單擊掃描按鈕
  2. onClick一個ZXing應用程序啟動
  3. 如果返回了正確的qr代碼,我們繼續進行其他用戶獲取手動輸入代碼的選項

我想用濃縮咖啡測試這個流程。 我想我必須使用有意或有意的1但我不知道如何檢查意圖是否是ZXing以及如何回到應用程序。

使用espresso-intents的一般流程如下:

  1. 調用intending(X).respondWith(Y)以設置模擬。
  2. 執行應導致意圖發送的操作。
  3. 調用intended(Z)以驗證模擬是否收到預期的意圖。

X和Z可以是相同的,但我傾向於使X盡可能概括(例如只匹配組件名稱),並使Z更具體(檢查額外值等)。

例如,對於ZXing,我可能會這樣做(警告:我沒有測試過這段代碼!):

Intents.intending(hasAction("com.google.zxing.client.android.SCAN"); // Match any ZXing scan intent
onView(withId(R.id.qr_scan_button).perform(click()); // I expect this to launch the ZXing QR scanner
Intents.intended(Matchers.allOf(
        hasAction("com.google.zxing.client.android.SCAN"),
        hasExtra("SCAN_MODE", "QR_CODE_MODE"))); // Also matchs the specific extras I'm expecting

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM