as far as I can see `action` is no longer availabl...
# spek
r
as far as I can see
action
is no longer available in 2.x what is now the idiomatic way of writing dependent tests? Following an example how I used it in spek 1.x:
Copy code
action("calling peek for the first time") {
    val obj = object : Any() {}
    val itr = listOf(obj).iterator().toPeekingIterator()
    val result = itr.peek()
    it("returns the element") {
        assert(result).isSameAs(obj)
    }
    it("calling it a second time still returns the same element") {
        val result2 = itr.peek()
        assert(result2).isSameAs(obj)
    }
    it("calling hasNext returns true") {
        assert(itr.hasNext()).toBe(true)
    }
    it("calling next returns the same element") {
        assert(itr.next()).isSameAs(obj)
    }
    it("calling then hasNext returns false") {
        assert(itr.hasNext()).toBe(false)
    }
}
that's what I came up with (quite a lot of boilerplate and way harder to read:
Copy code
context("calling peek for the first time") {
    val obj by memoized { object : Any() {} }
    lateinit var itr: PeekingIterator<Any>
    lateinit var result: Any
    beforeEachTest {
        itr = listOf(obj).iterator().toPeekingIterator()
        result = itr.peek()
    }
    it("it returns the element") {
        assert(result).isSameAs(obj)
    }
    context("calling it a second time") {
        lateinit var result2: Any
        beforeEachTest {
            result2 = itr.peek()
        }
        it("still returns the same element") {
            assert(result2).isSameAs(obj)
        }
        it("calling hasNext returns true") {
            assert(itr.hasNext()).toBe(true)
        }
        context("calling next") {
            lateinit var result3: Any
            beforeEachTest {
                result3 = itr.next()
            }
            it("returns the same element") {
                assert(result3).isSameAs(obj)
            }
            it("calling then hasNext returns false") {
                assert(itr.hasNext()).toBe(false)
            }
        }
    }
}
r
How about this (using
CachingMode.SCOPE
for memoized values):
Copy code
context("calling peek for the first time") {
    val obj by memoized(CachingMode.SCOPE) { object : Any() {} }
    val itr by memoized(CachingMode.SCOPE) { listOf(obj).iterator().toPeekingIterator() }
    lateinit var result: Any
    it("returns the element") {
        result = itr.peek()
        assert(result).isSameAs(obj)
    }
    it("calling it a second time still returns the same element") {
        val result2 = itr.peek()
        assert(result2).isSameAs(obj)
    }
    it("calling hasNext returns true") {
        assert(itr.hasNext()).toBe(true)
    }
    it("calling next returns the same element") {
        assert(itr.next()).isSameAs(obj)
    }
    it("calling then hasNext returns false") {
        assert(itr.hasNext()).toBe(false)
    }
}
r
that looks good 🙂 another question in the same area. There was a before/afterExecuteAction on the LifecycleListener. How do I implemented a scope based listener?
Forget my question, group scope is good enough