Dagger2可用于单元测试和仪表测试吗?

时间:2018-01-16 03:10:28

标签: android unit-testing dagger-2

https://developer.android.com/studio/test/index.html 您是否可以将Dagger2用于url = "some url constructed by substituting chunks in the URL quoted above" data = requests.get(url).json() for hit in data["hits"]["hit"]: """ hit is an object like this: {u'fields': {u'aluguel': u'1100', u'area': u'58', u'custo': u'1768', u'endereco': u'Rua Mariquinha Viana', u'foto_capa': u'capa892812990657_7908319390783DSC1351.JPG', u'id': u'892812990', u'photos': [u'892812990-657.7908319390783DSC1351.JPG', u'892812990-262.59010615240265DSC1352.JPG', u'892812990-981.8163710707928DSC1354.JPG', u'892812990-451.7013311422439DSC1355.JPG', u'892812990-173.347171880453DSC1356.JPG'], u'quartos': u'2', u'regiao_nome': u'Santana'}, u'id': u'892812990'} """ (位于module-name / src / test / java /。),Local unit tests(位于module-name / src / androidTest / java /。)或两者都使用?

有这方面的例子吗?

1 个答案:

答案 0 :(得分:1)

是。 Dagger2适用于单元测试和仪器化测试。这里有一个例子:https://github.com/googlesamples/android-architecture/tree/todo-mvp-dagger

以下是模拟变体中使用的模块示例,可用于单元/ ui测试:

@Module
abstract public class TasksRepositoryModule {

    private static final int THREAD_COUNT = 3;

    @Singleton
    @Binds
    @Local
    abstract TasksDataSource provideTasksLocalDataSource(TasksLocalDataSource dataSource);

    @Singleton
    @Binds
    @Remote
    abstract TasksDataSource provideTasksRemoteDataSource(FakeTasksRemoteDataSource dataSource);

    @Singleton
    @Provides
    static ToDoDatabase provideDb(Application context) {
        return Room.databaseBuilder(context.getApplicationContext(), ToDoDatabase.class, "Tasks.db")
                .build();
    }

    @Singleton
    @Provides
    static TasksDao provideTasksDao(ToDoDatabase db) {
        return db.taskDao();
    }

    @Singleton
    @Provides
    static AppExecutors provideAppExecutors() {
        return new AppExecutors(new DiskIOThreadExecutor(),
                Executors.newFixedThreadPool(THREAD_COUNT),
                new AppExecutors.MainThreadExecutor());
    }
}

https://github.com/googlesamples/android-architecture/blob/todo-mvp-dagger/todoapp/app/src/mock/java/com/example/android/architecture/blueprints/todoapp/data/source/TasksRepositoryModule.java#L24