Best Kotest code snippet using io.kotest.core.spec.style.scopes.BehaviorSpecGivenContainerScope
MovieDetailsSpec.kt
Source:MovieDetailsSpec.kt
...4import com.krzykrucz.fastfurious.support.KtorModuleTestSupport5import io.kotest.assertions.ktor.shouldHaveContent6import io.kotest.assertions.timing.eventually7import io.kotest.core.spec.style.BehaviorSpec8import io.kotest.core.spec.style.scopes.BehaviorSpecGivenContainerScope9import io.ktor.application.Application10import io.ktor.http.HttpMethod.Companion.Get11import io.ktor.server.testing.TestApplicationEngine12import io.ktor.server.testing.handleRequest13import kotlinx.coroutines.delay14import java.time.Duration15import java.time.Instant16import java.time.LocalDate17import java.time.Month.JUNE18import java.util.UUID19class MovieDetailsSpec : BehaviorSpec({20 `Given movie details app`(" and movie is added to catalog") { app ->21 app.movieIsAddedToCatalog(22 id = "tt0463985",23 title = "The Fast and the Furious: Tokyo Drift",24 description = "description",25 releaseDate = LocalDate.of(2006, JUNE, 16),26 runtime = Duration.ofMinutes(104),27 imdbRating = 6.128 )29 When("all details are fetched") {30 eventually {31 val response = app.handleRequest(Get, "/details").response32 Then("one movie details with no rating") {33 // language=JSON34 response shouldHaveContent """[{35 |"id":"tt0463985",36 |"title":"The Fast and the Furious: Tokyo Drift",37 |"description":"description",38 |"releaseDate":"2006-06-16",39 |"rating":"0.0",40 |"votes":"0",41 |"imdbRating":"6.1",42 |"runtime":"104 min"43 |}]""".trimMargin().toSingleLine()44 }45 }46 }47 }48 `Given movie details app`(" and movie is rated") { app ->49 app.movieIsAddedToCatalog(50 id = "tt0463985",51 title = "The Fast and the Furious: Tokyo Drift",52 description = "description",53 releaseDate = LocalDate.of(2006, JUNE, 16),54 runtime = Duration.ofMinutes(104),55 imdbRating = 6.156 )57 app.movieIsRated(58 id = "tt0463985",59 votesCount = 11,60 averageRating = 6.5161 )62 When("all details are fetched") {63 eventually {64 val response = app.handleRequest(Get, "/details").response65 Then("one movie details with rating updated") {66 // language=JSON67 response shouldHaveContent """[{68 |"id":"tt0463985",69 |"title":"The Fast and the Furious: Tokyo Drift",70 |"description":"description",71 |"releaseDate":"2006-06-16",72 |"rating":"6.5",73 |"votes":"11",74 |"imdbRating":"6.1",75 |"runtime":"104 min"76 |}]""".trimMargin().toSingleLine()77 }78 }79 }80 }81 `Given movie details app`(" and movie rated event is duplicated") { app ->82 app.movieIsAddedToCatalog(83 id = "tt0463985",84 title = "The Fast and the Furious: Tokyo Drift",85 description = "description",86 releaseDate = LocalDate.of(2006, JUNE, 16),87 runtime = Duration.ofMinutes(104),88 imdbRating = 6.189 )90 val eventId = UUID.randomUUID().toString()91 app.movieIsRated(92 eventId = eventId,93 id = "tt0463985",94 votesCount = 11,95 averageRating = 6.5196 )97 app.movieIsRated(98 eventId = eventId,99 id = "tt0463985",100 votesCount = 11,101 averageRating = 6.51102 )103 When("all details are fetched") {104 eventually {105 val response = app.handleRequest(Get, "/details").response106 Then("one movie details with rating updated") {107 // language=JSON108 response shouldHaveContent """[{109 |"id":"tt0463985",110 |"title":"The Fast and the Furious: Tokyo Drift",111 |"description":"description",112 |"releaseDate":"2006-06-16",113 |"rating":"6.5",114 |"votes":"11",115 |"imdbRating":"6.1",116 |"runtime":"104 min"117 |}]""".trimMargin().toSingleLine()118 }119 }120 }121 }122 `Given movie details app`(" and movie is rated twice") { app ->123 app.movieIsAddedToCatalog(124 id = "tt0463985",125 title = "The Fast and the Furious: Tokyo Drift",126 description = "description",127 releaseDate = LocalDate.of(2006, JUNE, 16),128 runtime = Duration.ofMinutes(104),129 imdbRating = 6.1130 )131 app.movieIsRated(132 id = "tt0463985",133 votesCount = 11,134 averageRating = 6.51135 )136 delay(50)137 app.movieIsRated(138 id = "tt0463985",139 votesCount = 12,140 averageRating = 5.1141 )142 When("all details are fetched") {143 eventually {144 val response = app.handleRequest(Get, "/details").response145 Then("one movie details with rating updated") {146 // language=JSON147 response shouldHaveContent """[{148 |"id":"tt0463985",149 |"title":"The Fast and the Furious: Tokyo Drift",150 |"description":"description",151 |"releaseDate":"2006-06-16",152 |"rating":"5.1",153 |"votes":"12",154 |"imdbRating":"6.1",155 |"runtime":"104 min"156 |}]""".trimMargin().toSingleLine()157 }158 }159 }160 }161 `Given movie details app`(" and movie is rated twice in wrong order") { app ->162 app.movieIsAddedToCatalog(163 id = "tt0463985",164 title = "The Fast and the Furious: Tokyo Drift",165 description = "description",166 releaseDate = LocalDate.of(2006, JUNE, 16),167 runtime = Duration.ofMinutes(104),168 imdbRating = 6.1169 )170 app.movieIsRated(171 id = "tt0463985",172 votesCount = 12,173 averageRating = 5.1174 )175 delay(50)176 app.movieIsRated(177 id = "tt0463985",178 votesCount = 11,179 averageRating = 6.51180 )181 When("all details are fetched") {182 eventually {183 val response = app.handleRequest(Get, "/details").response184 Then("one movie details with rating not updated") {185 // language=JSON186 response shouldHaveContent """[{187 |"id":"tt0463985",188 |"title":"The Fast and the Furious: Tokyo Drift",189 |"description":"description",190 |"releaseDate":"2006-06-16",191 |"rating":"5.1",192 |"votes":"12",193 |"imdbRating":"6.1",194 |"runtime":"104 min"195 |}]""".trimMargin().toSingleLine()196 }197 }198 }199 }200}) {201 override fun listeners() = listOf(ktor)202}203private fun TestApplicationEngine.movieIsAddedToCatalog(204 id: String,205 title: String,206 description: String,207 releaseDate: LocalDate,208 runtime: Duration,209 imdbRating: Double210) {211 environment.monitor.raise(212 IntegrationEvent.MovieAddedToCatalogEvent,213 IntegrationEvent.MovieAddedToCatalogEvent(214 id = id,215 imdbId = id,216 title = title,217 description = description,218 releaseDate = releaseDate,219 imdbRating = imdbRating,220 imdbVotes = 259_218,221 runtime = runtime,222 timestamp = anyTime223 )224 )225}226private fun TestApplicationEngine.movieIsRated(227 id: String,228 votesCount: Int,229 averageRating: Double,230 eventId: String = UUID.randomUUID().toString()231) {232 environment.monitor.raise(233 IntegrationEvent.MovieRatedEvent,234 IntegrationEvent.MovieRatedEvent(235 id = eventId,236 movieId = id,237 newAverageRating = averageRating,238 newVotesCount = votesCount,239 timestamp = anyTime240 )241 )242}243private fun BehaviorSpec.`Given movie details app`(244 name: String,245 test: suspend BehaviorSpecGivenContainerScope.(TestApplicationEngine) -> Unit246) = GivenApp("ratings app $name", ktor.moduleTestApp, test)247private val ktor = KtorModuleTestSupport(Application::movieDetailsModule, Unit)248private val anyTime = Instant.parse("2007-12-03T10:15:30.00Z")249private fun String.toSingleLine() =250 replace("[\n\r]".toRegex(), "")...
CatalogSpec.kt
Source:CatalogSpec.kt
...3import com.krzykrucz.fastfurious.support.GivenApp4import com.krzykrucz.fastfurious.support.KtorModuleTestSupport5import com.krzykrucz.fastfurious.support.OMDbApiMockTestSupport6import io.kotest.core.spec.style.BehaviorSpec7import io.kotest.core.spec.style.scopes.BehaviorSpecGivenContainerScope8import io.ktor.application.Application9import io.ktor.server.testing.TestApplicationEngine10import java.time.Clock11import java.time.Duration12import java.time.Instant13import java.time.LocalDate14import java.time.Month.APRIL15import java.time.Month.JUNE16import java.time.Month.MAY17import java.time.ZoneId18class CatalogSpec : BehaviorSpec({19 `Given catalog app`("") { app ->20 When("app is running") {21 Then("all movies are downloaded once") {22 omdbApiMock.verifyAllMoviesDownloadedOnce()23 }24 Then("all Fast&Furious movies are added to the catalog") {25 ktor.eventsShouldBePublished(26 MovieAddedToCatalogEvent(27 id = "tt0232500",28 imdbId = "tt0232500",29 title = "The Fast and the Furious",30 description = "Los Angeles police officer Brian O'Conner must decide where his loyalty really lies when he becomes enamored with the street racing world he has been sent undercover to destroy.",31 releaseDate = LocalDate.of(2001, JUNE, 22),32 imdbRating = 6.8,33 imdbVotes = 370_116,34 runtime = Duration.ofMinutes(106),35 timestamp = currentTime36 ),37 MovieAddedToCatalogEvent(38 id = "tt0322259",39 imdbId = "tt0322259",40 title = "2 Fast 2 Furious",41 description = "Former cop Brian O'Conner is called upon to bust a dangerous criminal and he recruits the help of a former childhood friend and street racer who has a chance to redeem himself.",42 releaseDate = LocalDate.of(2003, JUNE, 6),43 imdbRating = 5.9,44 imdbVotes = 264_489,45 runtime = Duration.ofMinutes(107),46 timestamp = currentTime47 ),48 MovieAddedToCatalogEvent(49 id = "tt0463985",50 imdbId = "tt0463985",51 title = "The Fast and the Furious: Tokyo Drift",52 description = "A teenager becomes a major competitor in the world of drift racing after moving in with his father in Tokyo to avoid a jail sentence in America.",53 releaseDate = LocalDate.of(2006, JUNE, 16),54 imdbRating = 6.0,55 imdbVotes = 259_218,56 runtime = Duration.ofMinutes(104),57 timestamp = currentTime58 ),59 MovieAddedToCatalogEvent(60 id = "tt1013752",61 imdbId = "tt1013752",62 title = "Fast & Furious",63 description = "Brian O'Conner, back working for the FBI in Los Angeles, teams up with Dominic Toretto to bring down a heroin importer by infiltrating his operation.",64 releaseDate = LocalDate.of(2009, APRIL, 3),65 imdbRating = 6.6,66 imdbVotes = 276_684,67 runtime = Duration.ofMinutes(107),68 timestamp = currentTime69 ),70 MovieAddedToCatalogEvent(71 id = "tt1596343",72 imdbId = "tt1596343",73 title = "Fast Five",74 description = "Dominic Toretto and his crew of street racers plan a massive heist to buy their freedom while in the sights of a powerful Brazilian drug lord and a dangerous federal agent.",75 releaseDate = LocalDate.of(2011, APRIL, 29),76 imdbRating = 7.3,77 imdbVotes = 368_801,78 runtime = Duration.ofMinutes(130),79 timestamp = currentTime80 ),81 MovieAddedToCatalogEvent(82 id = "tt1905041",83 imdbId = "tt1905041",84 title = "Fast & Furious 6",85 description = "Hobbs has Dominic and Brian reassemble their crew to take down a team of mercenaries: Dominic unexpectedly gets sidetracked with facing his presumed deceased girlfriend, Letty.",86 releaseDate = LocalDate.of(2013, MAY, 24),87 imdbRating = 7.0,88 imdbVotes = 383_258,89 runtime = Duration.ofMinutes(130),90 timestamp = currentTime91 ),92 MovieAddedToCatalogEvent(93 id = "tt2820852",94 imdbId = "tt2820852",95 title = "Furious 7",96 description = "Deckard Shaw seeks revenge against Dominic Toretto and his family for his comatose brother.",97 releaseDate = LocalDate.of(2015, APRIL, 3),98 imdbRating = 7.1,99 imdbVotes = 376_958,100 runtime = Duration.ofMinutes(137),101 timestamp = currentTime102 ),103 MovieAddedToCatalogEvent(104 id = "tt4630562",105 imdbId = "tt4630562",106 title = "The Fate of the Furious",107 description = "When a mysterious woman seduces Dominic Toretto into the world of terrorism and a betrayal of those closest to him, the crew face trials that will test them as never before.",108 releaseDate = LocalDate.of(2017, APRIL, 14),109 imdbRating = 6.6,110 imdbVotes = 221_120,111 runtime = Duration.ofMinutes(136),112 timestamp = currentTime113 ),114 MovieAddedToCatalogEvent(115 id = "tt5433138",116 imdbId = "tt5433138",117 title = "F9: The Fast Saga",118 description = "Dom and the crew must take on an international terrorist who turns out to be Dom and Mia's estranged brother.",119 releaseDate = LocalDate.of(2021, JUNE, 25),120 imdbRating = 5.2,121 imdbVotes = 106_141,122 runtime = Duration.ofMinutes(143),123 timestamp = currentTime124 ),125 )126 }127 }128 }129}) {130 override fun listeners() = listOf(omdbApiMock, ktor)131}132private fun BehaviorSpec.`Given catalog app`(133 name: String,134 test: suspend BehaviorSpecGivenContainerScope.(TestApplicationEngine) -> Unit135) = GivenApp("ratings app $name", ktor.moduleTestApp, test)136private val currentTime = Instant.parse("2007-12-03T10:15:30.00Z")137private val omdbApiMock = OMDbApiMockTestSupport()138private val testConfig = catalogConfig {139 clock = Clock.fixed(currentTime, ZoneId.systemDefault())140 omdbApiKey = omdbApiMock.apiKey141 omdbBaseUrl = omdbApiMock.baseUrl142}143private val ktor = KtorModuleTestSupport(Application::catalogModule, testConfig)...
RatingsSpec.kt
Source:RatingsSpec.kt
...6import io.kotest.assertions.ktor.shouldHaveContent7import io.kotest.assertions.ktor.shouldHaveStatus8import io.kotest.core.spec.Spec9import io.kotest.core.spec.style.BehaviorSpec10import io.kotest.core.spec.style.scopes.BehaviorSpecGivenContainerScope11import io.ktor.application.Application12import io.ktor.http.HttpMethod13import io.ktor.http.HttpStatusCode14import io.ktor.server.testing.TestApplicationEngine15import io.ktor.server.testing.handleRequest16import io.mockk.every17import io.mockk.mockk18import java.time.Clock19import java.time.Duration20import java.time.Instant21import java.time.LocalDate22import java.time.Month23import java.time.ZoneId24import java.util.UUID25class RatingsSpec : BehaviorSpec({26 `Given ratings app`(" and movies $someMovieId and $anotherMovieId are added to catalog") { app ->27 app.movieIsAddedToCatalog(someMovieId)28 app.movieIsAddedToCatalog(anotherMovieId)29 When("movie $someMovieId is rated") {30 val response = app.handleRequest(HttpMethod.Post, "/rate/$someMovieId/10").response31 Then("response status is ok") {32 response shouldHaveStatus HttpStatusCode.OK33 }34 Then("movie rated event is published") {35 ktor.eventShouldBePublished(36 IntegrationEvent.MovieRatedEvent(37 id = firstUUID.toString(),38 movieId = someMovieId,39 newAverageRating = 10.0,40 newVotesCount = 1,41 timestamp = currentTime42 )43 )44 }45 }46 When("movie $anotherMovieId is rated") {47 val response = app.handleRequest(HttpMethod.Post, "/rate/$someMovieId/5").response48 Then("response status is ok") {49 response shouldHaveStatus HttpStatusCode.OK50 }51 Then("movie rated event is published") {52 ktor.eventShouldBePublished(53 IntegrationEvent.MovieRatedEvent(54 id = secondUUID.toString(),55 movieId = someMovieId,56 newAverageRating = 7.5,57 newVotesCount = 2,58 timestamp = currentTime59 )60 )61 }62 }63 When("there is a call with invalid movie id") {64 val response = app.handleRequest(HttpMethod.Post, "/rate/f$someMovieId/5").response65 Then("response status is bad request") {66 response shouldHaveStatus HttpStatusCode.BadRequest67 response shouldHaveContent "movie id param is invalid"68 }69 }70 }71 `Given ratings app`(" and empty catalog") { app ->72 When("there is a call with invalid rating") {73 val response = app.handleRequest(HttpMethod.Post, "/rate/$unknownMovieId/10").response74 Then("response status is 5xx") {75 response shouldHaveStatus HttpStatusCode.InternalServerError76 response shouldHaveContent "NoSuchMovie"77 }78 Then("no events are published") {79 ktor.noEventsShouldBePublished()80 }81 }82 }83}) {84 override fun listeners() = listOf(ktor)85 override fun beforeSpec(spec: Spec) {86 super.beforeSpec(spec)87 every { uuidProvider() } returnsMany listOf(firstUUID, secondUUID)88 }89}90private fun TestApplicationEngine.movieIsAddedToCatalog(id: String) {91 environment.monitor.raise(92 IntegrationEvent.MovieAddedToCatalogEvent,93 IntegrationEvent.MovieAddedToCatalogEvent(94 id = id,95 imdbId = id,96 title = "The Fast and the Furious: Tokyo Drift",97 description = "A teenager becomes a major competitor in the world of drift racing after moving in with his father in Tokyo to avoid a jail sentence in America.",98 releaseDate = LocalDate.of(2006, Month.JUNE, 16),99 imdbRating = 6.0,100 imdbVotes = 259_218,101 runtime = Duration.ofMinutes(104),102 timestamp = currentTime - Duration.ofSeconds(1)103 )104 )105}106private val currentTime = Instant.parse("2007-12-03T10:15:30.00Z")107private const val someMovieId = "tt0463985"108private const val anotherMovieId = "tt1013752"109private const val unknownMovieId = "tt1905041"110private val firstUUID = UUID.fromString("b236a1b3-923d-42b1-a872-2eb1a2f8ef5d")111private val secondUUID = UUID.fromString("c0a0eff8-0b19-40ec-8286-a0ec712cdd39")112private val uuidProvider = mockk<UUIDProvider>()113private val testConfig = RatingsConfig(114 clock = Clock.fixed(currentTime, ZoneId.systemDefault()),115 uuidProvider = uuidProvider116)117private val ktor = KtorModuleTestSupport(Application::ratingsModule, testConfig)118private fun BehaviorSpec.`Given ratings app`(119 name: String,120 test: suspend BehaviorSpecGivenContainerScope.(TestApplicationEngine) -> Unit121) = GivenApp("ratings app $name", ktor.moduleTestApp, test)...
ShowtimesSpec.kt
Source:ShowtimesSpec.kt
...5import io.kotest.assertions.json.shouldMatchJson6import io.kotest.assertions.ktor.shouldHaveStatus7import io.kotest.core.spec.IsolationMode8import io.kotest.core.spec.style.BehaviorSpec9import io.kotest.core.spec.style.scopes.BehaviorSpecGivenContainerScope10import io.kotest.matchers.string.shouldMatch11import io.ktor.application.Application12import io.ktor.http.HttpMethod13import io.ktor.http.HttpStatusCode.Companion.OK14import io.ktor.server.testing.TestApplicationEngine15import io.ktor.server.testing.handleRequest16import io.ktor.server.testing.setBody17import java.time.Clock18import java.time.Duration19import java.time.Instant20import java.time.LocalDate21import java.time.Month22import java.time.ZoneId23class ShowtimesSpec : BehaviorSpec({24 `Given showtimes app`(" and movie $someMovieTitle is added to catalog") { app ->25 app.movieIsAddedToCatalog(someMovieTitle)26 var showId: String? = null27 When("show is created") {28 val response = app.handleRequest(HttpMethod.Post, "/showtimes/schedule") {29 addHeader("Content-Type", "application/json")30 setBody(31 """32 {33 "title": "$someMovieTitle",34 "showTime": "$showTime",35 "runtime": "$anyRuntime",36 "price": "$price"37 }38 """.trimIndent()39 )40 }.response41 Then("response status is ok") {42 response shouldHaveStatus OK43 response.content shouldMatch "([a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8})".toRegex()44 showId = response.content45 }46 Then("showtimes view contains this show") {47 val view = app.handleRequest(HttpMethod.Get, "/showtimes").response48 view shouldHaveStatus OK49 view.content shouldMatchJson """50 [51 {52 "day": "2007-12-04",53 "showtimes": {54 "The Fast and the Furious: Tokyo Drift": [55 "2007-12-04T11:15:30"56 ]57 }58 }59 ]60 """.trimIndent()61 }62 }63 When("show is updated") {64 val response = app.handleRequest(HttpMethod.Put, "/showtimes/schedule/$showId") {65 addHeader("Content-Type", "application/json")66 setBody(67 """68 {69 "startTime": "$showTimeHourLater",70 "price": "$priceDollarBigger"71 }""".trimIndent()72 )73 }.response74 Then("response status is ok") {75 response shouldHaveStatus OK76 }77 Then("showtimes view contains updated show") {78 val view = app.handleRequest(HttpMethod.Get, "/showtimes").response79 view shouldHaveStatus OK80 view.content shouldMatchJson """81 [82 {83 "day": "2007-12-04",84 "showtimes": {85 "The Fast and the Furious: Tokyo Drift": [86 "2007-12-04T12:15:30"87 ]88 }89 }90 ]91 """.trimIndent()92 }93 }94 }95}) {96 override fun listeners() = listOf(ktor)97 override fun isolationMode() = IsolationMode.SingleInstance98}99private fun TestApplicationEngine.movieIsAddedToCatalog(title: String) {100 environment.monitor.raise(101 IntegrationEvent.MovieAddedToCatalogEvent,102 IntegrationEvent.MovieAddedToCatalogEvent(103 id = "tt0463985",104 imdbId = "tt0463985",105 title = title,106 description = "A teenager becomes a major competitor in the world of drift racing after moving in with his father in Tokyo to avoid a jail sentence in America.",107 releaseDate = LocalDate.of(2006, Month.JUNE, 16),108 imdbRating = 6.0,109 imdbVotes = 259_218,110 runtime = Duration.ofMinutes(104),111 timestamp = currentTime - Duration.ofSeconds(1)112 )113 )114}115private val currentTime = Instant.parse("2007-12-03T10:15:30.00Z")116private const val someMovieTitle = "The Fast and the Furious: Tokyo Drift"117private const val anyRuntime = "PT1h"118private const val price = "11.54"119private const val priceDollarBigger = "12.54"120private const val showTime = "2007-12-04T11:15:30.00"121private const val showTimeHourLater = "2007-12-04T12:15:30.00"122private val testClock = Clock.fixed(currentTime, ZoneId.systemDefault())123private val ktor = KtorModuleTestSupport(Application::showtimesModule, testClock)124private fun BehaviorSpec.`Given showtimes app`(125 name: String,126 test: suspend BehaviorSpecGivenContainerScope.(TestApplicationEngine) -> Unit127) = GivenApp("showtimes app $name", ktor.moduleTestApp, test)...
BehaviorSpecGivenContainerScope.kt
Source:BehaviorSpecGivenContainerScope.kt
1package io.kotest.core.spec.style.scopes2import io.kotest.core.names.TestName3import io.kotest.core.spec.KotestTestScope4import io.kotest.core.test.TestScope5@Deprecated("This interface has been renamed to BehaviorSpecGivenContainerScope. Deprecated since 4.5")6typealias GivenScope = BehaviorSpecGivenContainerScope7@Deprecated("This interface has been renamed to BehaviorSpecGivenContainerScope. Deprecated since 5.0")8typealias BehaviorSpecGivenContainerContext = BehaviorSpecGivenContainerScope9/**10 * A context that allows tests to be registered using the syntax:11 *12 * when("some test")13 * when("some test").config(...)14 * xwhen("some disabled test")15 * xwhen("some disabled test").config(...)16 *17 * and18 *19 * then("some test")20 * then("some test").config(...)21 * xthen("some disabled test").config(...)22 * xthen("some disabled test").config(...)23 *24 */25@Suppress("FunctionName")26@KotestTestScope27class BehaviorSpecGivenContainerScope(28 val testScope: TestScope,29) : AbstractContainerScope(testScope) {30 suspend fun And(name: String, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) =31 addAnd(name, test, xdisabled = false)32 suspend fun and(name: String, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) =33 addAnd(name, test, xdisabled = false)34 suspend fun xand(name: String, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) =35 addAnd(name, test, xdisabled = true)36 suspend fun xAnd(name: String, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) =37 addAnd(name, test, xdisabled = true)38 private suspend fun addAnd(39 name: String,40 test: suspend BehaviorSpecGivenContainerScope.() -> Unit,41 xdisabled: Boolean42 ) {43 registerContainer(TestName("And: ", name, true), xdisabled, null) {44 BehaviorSpecGivenContainerScope(this).test()45 }46 }47 suspend fun When(name: String, test: suspend BehaviorSpecWhenContainerScope.() -> Unit) = addWhen(name, test, xdisabled = false)48 suspend fun `when`(name: String, test: suspend BehaviorSpecWhenContainerScope.() -> Unit) = addWhen(name, test, xdisabled = false)49 suspend fun xwhen(name: String, test: suspend BehaviorSpecWhenContainerScope.() -> Unit) = addWhen(name, test, xdisabled = true)50 suspend fun xWhen(name: String, test: suspend BehaviorSpecWhenContainerScope.() -> Unit) = addWhen(name, test, xdisabled = true)51 private suspend fun addWhen(name: String, test: suspend BehaviorSpecWhenContainerScope.() -> Unit, xdisabled: Boolean) {52 registerContainer(TestName("When: ", name, true), disabled = xdisabled, null) {53 BehaviorSpecWhenContainerScope(this).test()54 }55 }56 fun Then(name: String) = TestWithConfigBuilder(57 TestName("Then: ", name, true),58 this@BehaviorSpecGivenContainerScope,59 xdisabled = false60 )61 fun then(name: String) = TestWithConfigBuilder(62 TestName("Then: ", name, true),63 this@BehaviorSpecGivenContainerScope,64 xdisabled = false65 )66 fun xthen(name: String) = TestWithConfigBuilder(67 TestName("Then: ", name, true),68 this@BehaviorSpecGivenContainerScope,69 xdisabled = true70 )71 fun xThen(name: String) = TestWithConfigBuilder(72 TestName("Then: ", name, true),73 this@BehaviorSpecGivenContainerScope,74 xdisabled = true75 )76 suspend fun Then(name: String, test: suspend TestScope.() -> Unit) = addThen(name, test, xdisabled = false)77 suspend fun then(name: String, test: suspend TestScope.() -> Unit) = addThen(name, test, xdisabled = false)78 suspend fun xthen(name: String, test: suspend TestScope.() -> Unit) = addThen(name, test, xdisabled = true)79 suspend fun xThen(name: String, test: suspend TestScope.() -> Unit) = addThen(name, test, xdisabled = true)80 private suspend fun addThen(name: String, test: suspend TestScope.() -> Unit, xdisabled: Boolean) {81 registerTest(TestName("Then: ", name, true), disabled = xdisabled, null, test)82 }83}...
BehaviorSpecRootScope.kt
Source:BehaviorSpecRootScope.kt
...10 */11@Suppress("FunctionName")12interface BehaviorSpecRootScope : RootScope {13 /**14 * Adds a top level [BehaviorSpecGivenContainerScope] to this spec.15 */16 fun Given(name: String, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) = addGiven(name, false, test)17 /**18 * Adds a top level [BehaviorSpecGivenContainerScope] to this spec.19 */20 fun given(name: String, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) = addGiven(name, false, test)21 /**22 * Adds a top level disabled [BehaviorSpecGivenContainerScope] to this spec.23 */24 fun xgiven(name: String, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) = addGiven(name, true, test)25 /**26 * Adds a top level disabled [BehaviorSpecGivenContainerScope] to this spec.27 */28 fun xGiven(name: String, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) = addGiven(name, true, test)29 private fun addGiven(name: String, xdisabled: Boolean, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) {30 addContainer(31 TestName("Given: ", name, true),32 disabled = xdisabled,33 null34 ) { BehaviorSpecGivenContainerScope(this).test() }35 }36}...
KtorExtensions.kt
Source:KtorExtensions.kt
1package com.krzykrucz.fastfurious.support2import io.kotest.core.spec.style.BehaviorSpec3import io.kotest.core.spec.style.scopes.BehaviorSpecGivenContainerScope4import io.ktor.application.Application5import io.ktor.server.engine.ApplicationEngineEnvironment6import io.ktor.server.testing.TestApplicationEngine7import io.ktor.server.testing.createTestEnvironment8fun BehaviorSpec.GivenApp(9 name: String,10 appModule: Application.()->Unit,11 test: suspend BehaviorSpecGivenContainerScope.(TestApplicationEngine) -> Unit12) {13 Given(name) {14 withTestApplicationAsync(appModule) {15 this@Given.test(this)16 }17 }18}19suspend fun <R> withTestApplicationAsync(20 moduleFunction: Application.() -> Unit,21 test: suspend TestApplicationEngine.() -> R22): R {23 return withApplicationAsync(createTestEnvironment()) {24 moduleFunction(application)25 test()...
BehaviorSpecGivenContainerScope
Using AI Code Generation
1class BehaviorSpecGivenContainerScopeTest : BehaviorSpec({2class BehaviorSpecWhenContainerScopeTest : BehaviorSpec({3class BehaviorSpecThenContainerScopeTest : BehaviorSpec({4class BehaviorSpecAndContainerScopeTest : BehaviorSpec({5class BehaviorSpecButContainerScopeTest : BehaviorSpec({6class BehaviorSpecScenarioContainerScopeTest : BehaviorSpec({7class BehaviorSpecScenarioScopeTest : BehaviorSpec({8class BehaviorSpecGivenScopeTest : BehaviorSpec({9class BehaviorSpecWhenScopeTest : BehaviorSpec({10class BehaviorSpecThenScopeTest : BehaviorSpec({11class BehaviorSpecAndScopeTest : BehaviorSpec({12class BehaviorSpecButScopeTest : BehaviorSpec({13class BehaviorSpecThenScopeTest : BehaviorSpec({14class BehaviorSpecAndScopeTest : BehaviorSpec({15class BehaviorSpecButScopeTest : BehaviorSpec({16class BehaviorSpecThenScopeTest : BehaviorSpec({
BehaviorSpecGivenContainerScope
Using AI Code Generation
1class BehaviorSpecExample : BehaviorSpec({2Given("some context") {3When("some action is performed") {4Then("some testable outcome should be produced") {5And("some other testable outcome should be produced") {6}7}8}9}10})11class BehaviorSpecExample : BehaviorSpec({12Given("some context") {13When("some action is performed") {14Then("some testable outcome should be produced") {15And("some other testable outcome should be produced") {16}17}18}19}20})21class BehaviorSpecExample : BehaviorSpec({22Given("some context") {23When("some action is performed") {24Then("some testable outcome should be produced") {25And("some other testable outcome should be produced") {26}27}28}29}30})31class BehaviorSpecExample : BehaviorSpec({32Given("some context") {33When("some action is performed") {34Then("some testable outcome should be produced") {35And("some other testable outcome should be produced") {36}37}38}39}40})41class BehaviorSpecExample : BehaviorSpec({42Given("some context") {43When("some action is performed") {44Then("some testable outcome should be produced") {45And("some other testable outcome should be produced") {46}47}48}49}50})51class BehaviorSpecExample : BehaviorSpec({52Given("some context") {53When("some action is performed") {54Then("some testable outcome should be produced") {55And("some other testable outcome should be produced") {56}57}58}59}60})61class BehaviorSpecExample : BehaviorSpec({62Given("some context") {63When("some action is performed") {64Then("some testable outcome should be produced") {65And("some other testable outcome should be produced") {66}67}68}69}70})
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!