Why this tests sometimes is success and sometimes ...
# kotlintest
s
Why this tests sometimes is success and sometimes failed?
Copy code
fun loadData = MainScope().launch {
    view.showProgressDialog()
    // load data
}

Given(""){
    When(""){
        runBlocking {
            withContext(Dispatchers.Main) {
                presenter.loadData()
            }
        }

        Then("show progress dialog") {
            verify(exactly = 1) {
                view.showProgressDialog() 
            }
        }
    }
}
I am loading coroutines with this way
Copy code
val mainThreadSurrogate = newSingleThreadContext("Main thread")
override fun isolationMode(): IsolationMode = IsolationMode.InstancePerLeaf

override fun beforeSpec(spec: Spec) {
    super.beforeSpec(spec)
    Dispatchers.setMain(mainThreadSurrogate)
}

override fun afterSpec(spec: Spec) {
    super.afterSpec(spec)
    mainThreadSurrogate.close()
}
w
I recall having problems with blocks with the same name, do you also run this with
Given("")
and
When("")
? What if you tried putting two different, non-empty strings there?
s
that two have different string names
it was an example
👍 1
l
I believe the contexts are suspended. Why do you need a
RunBlocking
inside the test?
s
because i show it in examples of coroutines testing... 😅
have i remove runBlocking then?
l
I think you can, yes. Our Specs have
suspend
added to them, so they run in a coroutine context already
s
aaaaaaaah
dunno i could do that
l
I don't know if that's what causing your issues tho
s
you are talking about these
suspend When(""){}
?
l
You don't need
RunBlocking
inside your
When
s
ok i will try then
@LeoColman and withContext(Dispatchers.Main)? it must be removed?
l
That depends on your use case. I'm not sure why you're using that dispatcher
I don't think you would need it
s
I’d imagine your issue is that there’s some non-deterministic stuff going on
coroutines finishing first / second