question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Koin 1.0] Testing improvements with declareMock() & declare()

See original GitHub issue

Hello,

koin-test will no rely on mockito to provide out of the box mocking.

You can now directly declare a mock from a KoinTest file with declareMock():

val mod = module {
                single { ComponentA() }
                single { ComponentB(get()) }
            }
@Test
    fun `successful override factory with a mock`() {
        startKoin(listOf(mod))
        
       // will override ComponentA with mock definition
        declareMock<ComponentA>()
    }

You can also declare some components with declare { }, if you need more complex expression:

@Test
    fun `successful override factory with a mock`() {
        startKoin(listOf(mod))
        
        // will override ComponentA with mock definition
        declare { factory { DumbComponentA() } }
    }

No need to declare a special module, if you have only local expressions/mocks.

Available in koin-test version 1.0.0-beta-1.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
patrice-conilcommented, Jul 9, 2018

Sample updated with declareMock. Now , I’m waiting for the release 😉

1reaction
patrice-conilcommented, Jun 28, 2018

Thanks Arnaud, Now we have an nice way to do what we want I wrote a sample project using createMock that you can find here https://github.com/patrice-conil/testKoinMockBean Patrice

Read more comments on GitHub >

github_iconTop Results From Across the Web

Injecting in Tests - Koin
by inject() & get() - function to retrieve your instances from Koin ... declareMock & declare - to declare a mock or a...
Read more >
Mocking dependencies in UI test using Koin - ProAndroidDev
This is a simple way to mock runtime dependencies in Instrumentation tests when using Koin Library. First, let's take a look at how...
Read more >
Five Reasons Why You Should Use Koin in Your Next Android ...
Here are 5 reasons why you should rely on Koin in your new Android ... testCompile 'org.koin:koin-test:1.0.1' ... declareMock<TasksContract.
Read more >
CHANGELOG.md · Gitee 极速下载/koin - Gitee.com
Test. [ADDED] - asciidoc doc; [ADDED] - check() feature to check all configuration (does not create instances like dryRun); [ADDED] - declare mock...
Read more >
How to mock a koin injected bean in android activity test and ...
This sample demonstrates how to use koin declareMock() in androidTest to capture then call lambda given as parameter of one mocked bean.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found