【问题标题】:How to test screens which requires authorization? Espresso + MockWebServer如何测试需要授权的屏幕? Espresso + MockWebServer
【发布时间】:2019-09-19 01:15:27
【问题描述】:

我正在创建 UI 测试。为了不与真实服务器交互,我使用了 MockWebServer。我的目标是模拟各种服务器响应,并查看整个程序将如何响应它们。目前,我不明白如何打开需要授权的屏幕。当然,我可以编写一个登录到授权屏幕的代码,然后转到所需的窗口。但这需要额外的时间来完成测试,我想避免这种情况。我不想模拟类,因为我需要检查应用程序的生产版本。我该怎么做?

对于 DI,我使用 Dagger-2。下面是组件代码:

@Singleton
@Component(modules = {
        AvatarsModule.class,
        EncryptionModule.class,
        ApiModule.class,
        WalletsModule.class,
        GeneralModule.class,
        InteractorsModule.class,
        PushNotificationsModule.class,
        AppModule.class
})
public interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder context(Context context);
        AppComponent build();
    }

    void inject(App app);
}

这是存储授权状态的类代码:

public class ApiWrapper {
    private Api api;
    private KeyPair keyPair;
    private Account account; 

    ...

    public Flowable<Authorization> authorize(KeyPair tempKeyPair) {
        return api
                .authorize(tempKeyPair.getPublicKeyString().toLowerCase())
                .subscribeOn(Schedulers.io())
                .doOnNext((authorization -> {
                    this.account = authorization.getAccount();
                    this.keyPair = tempKeyPair;
                }));
    }
    ...
}

【问题讨论】:

    标签: android android-espresso dagger-2 mockwebserver


    【解决方案1】:

    如果有人仍然感兴趣。我编写了一个 InstrumentationTestFacade 类,我使用 Dagger 在其中放置了一个 ApiWrapper 对象。接下来,将 InstrumentationTestFacade 注入到 Application 对象中。由于应用程序对象不是单例,因此在主代码中没有责任泄漏,但是从测试代码中您可以使用以下代码访问此外观:

    Application application = (Application) InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();
    InstrumentationTestFacade facade = application.getInstrumentationTestFacade();
    

    下面是一个例子:

    public class InstrumentationTestFacade {
        private LogoutInteractor logoutInteractor;
        private SecurityInteractor securityInteractor;
        private ApiWrapper apiWrapper;
    
        public InstrumentationTestFacade(
                LogoutInteractor logoutInteractor,
                SecurityInteractor securityInteractor,
                ApiWrapper apiWrapper
        ) {
            this.logoutInteractor = logoutInteractor;
            this.securityInteractor = securityInteractor;
            this.apiWrapper = apiWrapper;
        }
    
        public void logout() {
            logoutInteractor.logout();
        }
    
        public ApiWrapper getApiWrapper() {
            return apiWrapper;
        }
    
        public SecurityInteractor getSecurityInteractor() {
            return this.securityInteractor;
        }
    }
    
    public class Application extends MultiDexApplication implements HasActivityInjector, HasServiceInjector {
    
        ...
    
        @Inject
        InstrumentationTestFacade instrumentationTestFacade;
    
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            DaggerAppComponent
                    .builder()
                    .context(this)
                    .build()
                    .inject(this);
    
        }
    
        ...
    
        public InstrumentationTestFacade getInstrumentationTestFacade() {
            return instrumentationTestFacade;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-01
      • 1970-01-01
      • 2020-12-16
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 2021-07-10
      相关资源
      最近更新 更多