gpt4 book ai didi

android-espresso - 模拟服务器请求Android Espresso UI测试

转载 作者:行者123 更新时间:2023-12-01 00:35:40 27 4
gpt4 key购买 nike

我正在使用Espresso为我的Android应用程序编写UI测试,并想使用MockWebServer模拟HTTP请求。
在运行测试之前,我需要模拟身份验证响应并登录用户。

有没有一种方法可以使应用程序使用模拟网络服务器,以便发出实际的请求,我可以使用在模拟网络服务器上排队的响应。

到目前为止,我有:

public class AuthenticationTest {

@Rule
public ActivityTestRule<Authentication> mActivityTestRule = new ActivityTestRule<>(Authentication.class);

private Authentication activity;
private MockWebServer server;

@Before
public void signin() throws Exception {
server = new MockWebServer();
server.start();
activity = mActivityTestRule.getActivity();
MyApplication.State state = activity.getState();

String serverUrl = server.url("/").toString();

// Here is where I have a problem. How to force client to use mock server?

}

@Test
public void firstTest() {
String contentType = "Content-type: application/json";
MockResponse r1 = new MockResponse().setResponseCode(200).setBody("example_body").addHeader(contentType);
server.enqueue(r1);

// typing credentials and pressing "Sign in" button, which should use mocked server's response:

ViewInteraction email = onView(allOf(withId(R.id.emailAddress), isDisplayed()));
email.perform(replaceText("some_email@test.com"), closeSoftKeyboard());
ViewInteraction password = onView(allOf(withId(R.id.password), isDisplayed()));
password.perform(replaceText("some_password"), closeSoftKeyboard());
ViewInteraction signin = onView(allOf(withId(R.id.signInButton), withText("Sign In"), isDisplayed()));
button2.perform(click());
}

最佳答案

用Dagger替换依赖项的This example。但是您可以使用任何其他方法进行DI。主要思想-通过自定义测试运行器提供应用程序的“测试”版本来替换测试期间的依赖关系。

关于android-espresso - 模拟服务器请求Android Espresso UI测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41386652/

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