2016-07-09 20 views
6

Il Dagger 2 documentation suggests providing different configurations per il test e la produzione utilizzando un interface per ProductionComponent e TestComponent, come segue:Test con Dagger 2 utilizzando configurazioni dei componenti separati in Android

@Component(modules = { 
    OAuthModule.class, // real auth 
    FooServiceModule.class, // real backend 
    OtherApplicationModule.class, 
    /* … */ }) 
interface ProductionComponent { 
    Server server(); 
} 

@Component(modules = { 
    FakeAuthModule.class, // fake auth 
    FakeFooServiceModule.class, // fake backend 
    OtherApplicationModule.class, 
    /* … */}) 
interface TestComponent extends ProductionComponent { 
    FakeAuthManager fakeAuthManager(); 
    FakeFooService fakeFooService(); 
} 

Diciamo che abbiamo un'attività Android (MyApp), che utilizza ProductionComponent:

public class MyApp extends Application { 
    private ProductionComponent component; 

    @Override public void onCreate() { 
     super.onCreate(); 

     component = ProductionComponent.builder() 
       .serverModule(new ServerModule()) 
       .build(); 
    } 
} 

in generale, qual è il modo migliore per utilizzare DaggerTestComponent.builder() piuttosto che ProductionComponent.builder() in Androi d test di integrazione?

Non sono sicuro di come utilizzare i falsi; Devo fare una nuova attività in /androidTest quale extends MyApp? O devo passare un nuovo a MyApp utilizzando un getter/setter quando imposto il mio test?

+1

trovato una soluzione molto utile a https://blog.egorand.me/providing-test-doubles-with-dagger-1-and-dagger-2/ – user2560886

risposta

Problemi correlati