TegralSubjectTest

Base class for testing Tegral DI-applications.

The basic idea behind this class is to provide testing facilities around a single "subject". Let's say we wanted to test a service and its interactivity with a repository. Our subject here would be the service.

The class itself provides a wrapper around an UnsafeMutableEnvironment that is well-suited for test scenarios.

Here is what a typical use of this class could look like, with the MockK library for mocking our repository.

// Main code
interface Repository {
fun storeThis(text: String)
}

class Service(scope: InjectionScope) {
private val repository: Repository by scope()

fun incomingText(text: String) {
// ...
repository.storeThis(text)
// ...
}
}

// Test code
class TestService : TegralDiBaseTest<Service>(::Service) {
@Test
fun `Accepts incoming text properly`() = test {
put<Repository> {
mockk { every { storeThis("hello") } just runs }
}

subject.incomingText("hello")

verify { get<Repository>().storeThis("hello") }
}
}

The class defines the environment using a base module that usually only contains the test subject's entry, but may also contain additional dependencies as you see fit. Refer to the different constructors for more information.

Parameters

subjectClass

The class of the subject, used for subject to work properly.

baseModule

The base module as an InjectableModule instance.

Constructors

Link copied to clipboard
fun <TSubject : Any> TegralSubjectTest(subjectClass: KClass<TSubject>, baseModuleBuilder: ContextBuilderDsl.() -> Unit)

This constructor takes a module builder and the subject's class, builds the module then uses that as a base.

Link copied to clipboard

Shortcut for cases where you want to create a single-component module. For example, this:

Link copied to clipboard
fun <TSubject : Any> TegralSubjectTest(subjectClass: KClass<TSubject>, baseModule: InjectableModule)

This constructor takes a pre-built base module and the class of the subject and uses them as a base.