Skip to content

Commit

Permalink
Merge pull request #93 from fknives/issue#92-rule-chain
Browse files Browse the repository at this point in the history
Issue#92 Add notes about the Koin.reset + NetworkMocking rule
  • Loading branch information
fknives authored Jul 1, 2022
2 parents 91ff86a + a935c23 commit 8cd3ddc
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions codekata/sharedtests.instructionset.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,28 +303,66 @@ private val testDispatcher: TestDispatcher get() = mainDispatcherRule.testDispat
```
>Notice we are using get function! so we don't access the dispatcher before our tests are run!
Not all we need to do is remove our previous dispatcher setups.
Now all we need to do is remove our previous dispatcher setups.

##### MockNetwork and Koin reset Rule

For setting up the MockServer and reseting the Koin, there is already a rule prepared for us: `org.fnives.test.showcase.testutils.MockServerScenarioSetupResetingTestRule`.

We will use this also to decluter our `setup` and `tearDown` function.

First let's remove what's no longer necessary:
```kotlin
@Before
fun setup() {
robot = CodeKataSharedRobotTest()
activityScenario = ActivityScenario.launch(AuthActivity::class.java)
activityScenario.moveToState(Lifecycle.State.RESUMED)
}

@After
fun tearDown() {
activityScenario.safeClose()
}
```

And apply our rule:
```kotlin
@Rule
@JvmField
val mockServerAndKoinRule = MockServerScenarioSetupResetingTestRule()
private val mockServerScenarioSetup: MockServerScenarioSetup get() = mockServerAndKoinRule.mockServerScenarioSetup
```

##### Rule order

What order our Rules are applied? Well, frankly I have no idea, that's because we can be explicit about our Rule's order, via RuleChain, so I always use that.
What order our Rules are applied? Well, frankly I have no idea, that's because we can be explicit about our Rule's order, via RuleChain, so I always use that instead.
> Note our rule order is important, because Database and MockServer should happen after Koin reset.
I recommend doing this, so it's easier to read your test classes and being explicit what happens in what order.

So how would that look like:
```kotlin
private lateinit var activityScenario: ActivityScenario<AuthActivity>
private lateinit var robot: CodeKataSharedRobotTest

// rules
private val intentRule = CodeKataIntentInitRule()
private val mainDispatcherRule = CodeKataMainDispatcherRule()
private val testDispatcher: TestDispatcher get() = mainDispatcherRule.testDispatcher
private val mockServerAndKoinRule = MockServerScenarioSetupResetingTestRule()
private val mockServerScenarioSetup: MockServerScenarioSetup get() = mockServerAndKoinRule.mockServerScenarioSetup

@Rule
@JvmField
val ruleOrder: RuleChain = RuleChain.outerRule(intentRule)
.around(mockServerAndKoinRule)
.around(mainDispatcherRule)
```

> Notice: we removed the Rule annotations, and only have one Rule, the RuleChain.
> Notice: we removed the Rule annotations from the others, and only have one Rule, the RuleChain.
The Rule chain starts our `intentRule` first, then the `mainDispatcherRule`, when cleaning up, first `mainDispatcherRule` will clean up then `intentRule`. That's because of the Statements, since one statement calls the other's evaluation. So the `IntentRule` received now the `mainDispatcherRule`'s statement and it calls evaluate on it.
The Rule chain starts our `intentRule` first, then the `mockServerAndKoinRule` and `mainDispatcherRule`, when cleaning up, first `mainDispatcherRule` will clean up, then the `mockServerAndKoinRule` and lastly then `intentRule`. That's because of the Statements, since one statement calls the other's evaluation. So the `IntentRule` received now the `mockServerAndKoinRule`'s statement, and the `mockServerAndKoinRule` received the `mainDispatcherRule`'s statement and they call evaluate on it.

*TLDR: The rules are applied in the order in which they are added to the RuleChain*

Expand Down

0 comments on commit 8cd3ddc

Please sign in to comment.