This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package sample | |
| import com.github.jeremyrempel.yaba.util.runBlockingTest | |
| import io.ktor.client.HttpClient | |
| import io.ktor.client.engine.mock.MockEngine | |
| import io.ktor.client.engine.mock.respond | |
| import io.ktor.client.features.json.JsonFeature | |
| import io.ktor.client.features.json.serializer.KotlinxSerializer | |
| import io.ktor.client.request.get | |
| import kotlinx.serialization.Serializable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @RunWith(AndroidJUnit4::class) | |
| class MainFragmentTest { | |
| | |
| @MockK | |
| private lateinit var fakeViewModel: MainFragmentViewModel | |
| | |
| @Before | |
| fun setUp() = MockKAnnotations.init(this) | |
| | |
| @Test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FakeViewModelFactory(private val testviewModel: ViewModel) : ViewModelProvider.Factory { | |
| override fun <T : ViewModel?> create(modelClass: Class<T>) = testviewModel as T | |
| } | |
| | |
| class FragFactoryFake(private val viewModel: ViewModel) : FragmentFactory() { | |
| override fun instantiate(classLoader: ClassLoader, className: String): Fragment { | |
| val fakeViewModelFactory = FakeViewModelFactory(viewModel) | |
| return MainFragment(fakeViewModelFactory) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DaggerComponent | |
| .builder() | |
| .databaseModule.create(DatabaseModule(appContext)) | |
| .build() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component DatabaseModule(appContext: Context) { | |
| @Provides | |
| @Singleton | |
| fun providesRoom: Room { | |
| return Room | |
| .databaseBuilder(appContext, AppDatabase::class.java, "database-name") | |
| .build | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyApplication: Application() { | |
| lateinit var dagger: MyComponent | |
| | |
| override fun onCreate() { | |
| super.onCreate(); | |
| dagger = DaggerMyComponent.create(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Module | |
| class ServiceModule { | |
| @Provides | |
| @Singleton | |
| fun providesRetrofit(): Retrofit { | |
| return Retrofit.Builder() | |
| .baseUrl("https://api.github.com/") | |
| .build(); | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MainActivity : AppCompatActivity() { | |
| | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| // init dagger graph | |
| val dagger: MyComponent = DaggerMyComponent.create() | |
| | |
| // insert the fragment factory | |
| supportFragmentManager.fragmentFactory = dagger.fragFactory() | |
| | |
| super.onCreate(savedInstanceState) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component(modules = [MyFactoryModule::class]) | |
| interface MyComponent { | |
| fun fragFactory(): FragmentFactory | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Module | |
| class MyFactoryModule { | |
| @Provides | |
| fun providesModelFactory(modelFactory: MyViewModelFactory): ViewModelProvider.Factory = modelFactory | |
| | |
| @Provides | |
| fun providesFragFactory(fragFactory: MyFragmentFactory): FragmentFactory = fragFactory | |
| } |