Created
March 12, 2018 08:59
-
-
Save rozkminiacz/0fa314ba326fa1018ae53075bfd6eb06 to your computer and use it in GitHub Desktop.
Model View Presenter Writing Spek Specification - gists for blog post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
(...) | |
testImplementation unitTestDependencies.values() | |
testImplementation spekDependencies.values() | |
testImplementation kotlinTestDependencies.values() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface PeopleListContract { | |
interface View { | |
fun addPeople(people: List<Person>) | |
fun showError(throwable: Throwable = Throwable()) | |
fun showLoading() | |
fun hideLoading() | |
} | |
interface Presenter { | |
fun attach(view: View) | |
fun detach() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PeopleListPresenter(private val repository: Repository<Person>) : PeopleListContract.Presenter { | |
var view: PeopleListContract.View? = null | |
override fun attach(view: PeopleListContract.View) { | |
this.view = view | |
loadPeople() | |
} | |
private fun loadPeople() { | |
repository.getAll() | |
.doOnSubscribe { view?.showLoading() } | |
.doOnError { | |
view?.apply { | |
showError(it) | |
hideLoading() | |
} | |
} | |
.doOnSuccess { | |
view?.apply { | |
addPeople(it) | |
hideLoading() | |
} | |
} | |
.subscribe() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PeopleListPresenter(val repository: Repository<Person>) : PeopleListContract.Presenter { | |
var view: PeopleListContract.View? = null | |
override fun attach(view: PeopleListContract.View) { | |
this.view = view | |
} | |
override fun detach() { | |
this.view = null | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val presenter = PeopleListPresenter(repository) | |
on("presenter attach"){ | |
presenter.attach(view) | |
it("should attach view"){ | |
assertNotNull(presenter.view) | |
} | |
it("should start loading") { | |
verify(view).showLoading() | |
} | |
it("should load list into view"){ | |
verify(view).addPeople(peopleList) | |
} | |
it("should stop loading") { | |
verify(view).hideLoading() | |
} | |
it("should never show any error"){ | |
verify(view, never()).showError(anyOrNull()) | |
} | |
} | |
on("presenter detach"){ | |
it("should detach view"){ | |
presenter.detach() | |
assertNull(presenter.view) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
given("no people in repository"){ | |
val view: PeopleListContract.View = mock() | |
val repository : Repository<Person> = mock { | |
on { getAll() } doReturn Single.error(Error("no data")) | |
} | |
val presenter = PeopleListPresenter(repository) | |
on("presenter attach"){ | |
presenter.attach(view) | |
presenterAttachStandardFlow(view, presenter){ | |
it("should not add anything to view"){ | |
verify(view, never()).addPeople(any()) | |
} | |
it("should display error in view"){ | |
verify(view).showError(any()) | |
} | |
} | |
} | |
presenterDetachAction(presenter) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val peopleList : List<Person> = listOf( | |
Person(id = "1", firstName = "Mark", surname = "Train"), | |
Person(id = "2", firstName = "George", secondName = "Edward", surname = "Bicycle") | |
) | |
val repository : Repository<Person> = mock{ | |
on { getAll() } doReturn Single.just(peopleList) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
given("no people in repository"){ | |
val view: PeopleListContract.View = mock() | |
val repository : Repository<Person> = mock { | |
on { getAll() } doReturn Single.error(Error("no data")) | |
} | |
val presenter = PeopleListPresenter(repository) | |
on("presenter attach"){ | |
presenter.attach(view) | |
presenterAttachStandardFlow(view, presenter){ | |
it("should not add anything to view"){ | |
verify(view, never()).addPeople(any()) | |
} | |
it("should display error in view"){ | |
verify(view).showError(any()) | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on("presenter attach") { | |
presenter.attach(view) | |
it("should attach view") { | |
assertNotNull(presenter.view) | |
} | |
it("should start loading") { | |
verify(view).showLoading() | |
} | |
it("should load list into view") { | |
verify(view).addPeople(peopleList) | |
} | |
it("should stop loading") { | |
verify(view).hideLoading() | |
} | |
it("should never show any error") { | |
verify(view, never()).showError(anyOrNull()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jetbrains.spek.api.Spek | |
import org.jetbrains.spek.api.dsl.* | |
import org.junit.platform.runner.JUnitPlatform | |
import com.nhaarman.mockito_kotlin.* | |
import org.junit.runner.RunWith | |
@RunWith(JUnitPlatform::class) | |
object PeopleListTest : Spek({ | |
given("people exists in repository"){ | |
val view: PeopleListContract.View = mock() | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on("presenter attach") { | |
presenter.attach(view) | |
presenterAttachStandardFlow(view, presenter) { | |
it("should add people to view") { | |
verify(view).addPeople(peopleList) | |
} | |
it("should never show error") { | |
verify(view, never()).showError(anyOrNull()) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun ActionBody.presenterAttachStandardFlow( | |
view: PeopleListContract.View, | |
presenter: PeopleListPresenter, | |
additionalTestBlock: () -> Unit) { | |
it("should attach view") { | |
Assert.assertNotNull(presenter.view) | |
} | |
it("should start loading") { | |
verify(view).showLoading() | |
} | |
additionalTestBlock.invoke() | |
it("should stop loading") { | |
verify(view).hideLoading() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun SpecBody.detachPresenterAction(presenter: PeopleListPresenter) { | |
on("presenter detach") { | |
presenter.detach() | |
it("should detach view") { | |
Assert.assertNull(presenter.view) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Repository<in T>{ | |
fun getAll() : Single<List<T>> | |
fun getOne(id : String) : Single<T> | |
fun create(t : T) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on("presenter detach"){ | |
presenter.detach() | |
it("should dispose observables"){ | |
// | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ext { | |
def JunitVersion = '4.12' | |
def KotlinMockitoVersion = '1.5.0' | |
def SpekVersion = "1.1.5" | |
unitTestDependencies = [ | |
junit : "junit:junit:${JunitVersion}", | |
] | |
kotlinTestDependencies = [ | |
kotlinMockito: "com.nhaarman:mockito-kotlin:${KotlinMockitoVersion}", | |
] | |
spekDependencies = [ | |
spekApi: "org.jetbrains.spek:spek-api:${SpekVersion}", | |
platformEngine: "org.jetbrains.spek:spek-junit-platform-engine:${SpekVersion}", | |
junitPlatformRunner: "org.junit.platform:junit-platform-runner:1.0.0" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment