Skip to content

Commit

Permalink
Make default InstanceFactory more lenient
Browse files Browse the repository at this point in the history
Resolves #171, no-arg secondary constructors will
now also be considered. If you have multiple no-arg
constructors, behaviour is undefined.
  • Loading branch information
raniejade committed Jan 29, 2017
1 parent 6a1c5a3 commit c35f478
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class SpekTestEngine: HierarchicalTestEngine<SpekExecutionContext>() {

val defaultInstanceFactory = object: InstanceFactory {
override fun <T: Spek> create(spek: KClass<T>): T {
return spek.objectInstance ?: spek.primaryConstructor!!.call()
return spek.objectInstance ?: spek.constructors.first { it.parameters.isEmpty() }
.call()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ class InstanceFactoryTest: AbstractSpekTestEngineTest() {
}
}

@Test
fun testDefaultUsingSecondaryConstructors() {
class SomeSpec(number: Int): Spek({
it("should be $number") { }
}) {
constructor(): this(10)
}

val recorder = executeTestsForClass(SomeSpec::class)
assertThat(recorder.testSuccessfulCount, equalTo(1))
}

@Test
fun testUsingObject() {
@CreateWith(SimpleFactoryAsAnObject::class)
Expand Down

0 comments on commit c35f478

Please sign in to comment.