2016-07-09 35 views
6

單獨的組件配置匕首2 documentation suggests providing different configurations用於測試和生產使用interfaceProductionComponentTestComponent,具體如下:測試用匕首2中使用的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(); 
} 

比方說,我們有一個Android的活動(MyApp)的使用ProductionComponent

public class MyApp extends Application { 
    private ProductionComponent component; 

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

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

一般來說,什麼在Androi使用DaggerTestComponent.builder()而非ProductionComponent.builder()的最佳方式d集成測試?

我不確定如何使用假貨;我應該在/androidTest中進行一項新活動,其中extends MyApp?或者我應該在設置我的測試時使用getter/setter將新的DaggerTestComponent傳入MyApp

+1

引用在https://blog.egorand.me/providing-test-doubles-with-dagger-1-and-dagger-2/找到了一個非常有用的解決方案 – user2560886

回答