【问题标题】:Stubbing/mocking intent in Android Espresso testAndroid Espresso 测试中的存根/模拟意图
【发布时间】:2015-11-06 10:32:01
【问题描述】:

我想在我的应用程序中测试以下流程:

  1. 用户点击扫描按钮
  2. onClick 启动 ZXing 应用
  3. 如果返回正确的二维码,我们将继续,否则用户可以选择手动输入二维码

我想用浓缩咖啡来测试这个流程。我想我必须使用有意或有意的1,但我不确定如何检查意图是否是 ZXing 以及如何返回应用程序。

【问题讨论】:

    标签: java zxing android-espresso stubbing


    【解决方案1】:

    使用 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
    

    【讨论】:

    • 未使用 ZXing 测试,但在我的示例中使用 ACTION_SEND
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多