Hello! How would I apply a JUnit Rule with Spek? E...
# spek
q
Hello! How would I apply a JUnit Rule with Spek? Example:
Copy code
internal class StatsViewModelSpek : Spek({
    describe("a StatsViewModel") {
        beforeEachTest {
            // Junit Rule to apply 
            InstantTaskExecutorRule()
        }
        context("") {
            val stats = Stats()
            on("create") {
                it("should initialize without problems") {
                    val statsViewModel = StatsViewModel(stats)
                }
            }
        }
    }
})
This returns
java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked
, because I use
LiveData
inside that viewModel
So instead of calling
InstantTaskExecutorRole()
I added this
Copy code
beforeEachTest {
            // Junit Rule to apply
            ArchTaskExecutor.getInstance().setDelegate(object : TaskExecutor() {
                override fun executeOnDiskIO(runnable: Runnable) {
                    runnable.run()
                }

                override fun isMainThread(): Boolean {
                    return true
                }

                override fun postToMainThread(runnable: Runnable) {
                    runnable.run()
                }
            })
        }
and that does the job
Also
afterEachTest { ArchTaskExecutor.getInstance().setDelegate(null) }
just to be doing the same thing as the rule