gpt4 book ai didi

android - 使用 Espresso stub Intent 时出错

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:08 27 4
gpt4 key购买 nike

我有两个通过 Intent 相互交互的应用程序。我想验证一下 App A 是否正确调用了 App BstartActivity 而无需实际启动 App B .我已经尝试了 intending 的各种组合,Espresso 仍然通过 intent 启动 App B 而不是简单地将其删除。这会导致其余测试失败,因为 UI 被 App B 阻止了。有什么想法吗?

@RunWith( AndroidJUnit4.class )
@LargeTest
public class MyActivityUiIntentsTest
{

@Rule
public IntentsTestRule<MyActivity> activityRule =
new IntentsTestRule<>( MyActivity.class, true, false );

@Test
public void shouldStartOtherActivityWhenButtonClicked ()
{
Intents.init();
intending( toPackage( "my.package" ) )
.respondWith( new ActivityResult( Activity.RESULT_OK, null ) );

activityRule.launchActivity( new Intent() );

onView( withId( R.id.viewId ) ).perform( click() );
intended( hasComponent( hasShortClassName( "the.other.class.name" ) ) );

Intents.release();
}
}

更新 onClick 代码:

@OnClick( R.id.viewId )
public void startOtherActivity ()
{
Intent intent = new Intent();
intent.setClassName( "my.package", "the.other.class.name" );
startActivity( intent );
finish();
}

最佳答案

将您的 intending... 代码移到 launchActivity 下方并删除 .init() 因为 IntentsTestRule 会在 Activity 结束后为您调用 init推出

关于android - 使用 Espresso stub Intent 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36753541/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com