dave08
11/28/2022, 2:20 PMThe test application has already been built. Make sure you configure the application before accessing the client for the first time.
🤕... I wish Ktor would just let me create my own testApplication
without trying to magically find an existing one...e5l
11/28/2022, 2:21 PMdave08
11/28/2022, 2:23 PMtestApplication {
environment { this.config = MapApplicationConfig() }
application {
this@testApplication.install(Authentication) { basic { skipWhen { true } } }
routing {
route("crm") {
registrationRoutes.externalRoutes(this)
}
}
}
...
dave08
11/28/2022, 2:23 PMdave08
11/28/2022, 2:25 PMfun main(args: Array<String>) {
val env = applicationEngineEnvironment {
module {
val component = DaggerAppComponent.create()
<http://component.app|component.app>(this)
}
// Private API
connector {
host = "0.0.0.0"
port = 8080
}
// Public API
connector {
host = "0.0.0.0"
port = 8050
}
}
embeddedServer(Netty, env).start(true)
}
dave08
11/28/2022, 2:25 PMdave08
11/28/2022, 2:27 PMfun Application.module() {}
to the source in the module where the test code is... maybe that's what's causing the problem?dave08
11/28/2022, 2:28 PMdave08
11/28/2022, 3:38 PMtestApplication {
environment {
config = MapApplicationConfig()
module {
this@module.install(Authentication) { basic("myBasicAuth") { skipWhen { true } } }
routing {
route("crm") {
registrationRoutes.externalRoutes(this)
}
}
}
}
But this REALLY wasn't obvious from the docs... that you need to use only ONE of the blocks environment, application, or routes in testApplication...dave08
11/28/2022, 3:39 PMAleksei Tirman [JB]
11/28/2022, 6:33 PMdave08
11/28/2022, 8:22 PMdave08
11/28/2022, 8:23 PMdave08
11/29/2022, 4:04 AMAleksei Tirman [JB]
11/29/2022, 2:30 PMdave08
11/29/2022, 2:31 PMdave08
11/29/2022, 2:32 PMapplication { }
, environment { }
, or routing { }
in the same testApplication { }
blockRustam Siniukov
11/29/2022, 2:33 PMthis@testApplication.
in the example above?dave08
11/29/2022, 2:35 PMwhy do you needI didn't, I just didn't know from where to take thein the example above?this@testApplication.
install()
function... so I needed to use trial and error... + the extra problem with multiple config blocks...dave08
11/29/2022, 2:38 PMtestApplication { }
... it's hard to track down how it actually gets configured, when really I just needed a fresh instance to build myself... I don't really like having config files for that, since I need to inject fake fixtures inside my routing classes...dave08
11/29/2022, 2:56 PMRustam Siniukov
11/29/2022, 3:03 PMtestApplication
block that it will load your config file if you have one by default. It is explicitly listed in the doc and there is also example on how to disable it.
Your problem here that you explicitly call function from the outer scope. You could just write application { install(...) }
and it would work.Rustam Siniukov
11/29/2022, 3:06 PMinstall
in top level inside testApplication { ... }
. But in your example you mixed these two.dave08
11/29/2022, 3:06 PM@KtorDsl
public fun environment(block: ApplicationEngineEnvironmentBuilder.() -> Unit) {
checkNotBuilt()
environmentBuilder = block
}
/**
* Adds a module to [TestApplication].
* @see [testApplication]
*/
@KtorDsl
public fun application(block: Application.() -> Unit) {
checkNotBuilt()
applicationModules.add(block)
}
...
dave08
11/29/2022, 3:07 PMRustam Siniukov
11/29/2022, 3:08 PMbuilt
flag is set, you’ll see that it happens only in accessing environment, not on the install.
Again, your code crashed because you explicitly called outer function in the inner scopodave08
11/29/2022, 3:09 PMinstall()
from the outer scope does try to access the environment...?Rustam Siniukov
11/29/2022, 3:12 PMinstall(…)
is a shortcut for application { install(...) }
. So when you call ``application { this@testApplication.install(...) }` it is the same as application { application { install(...) } }
. We can support it by simple fix, but we haven’t seen this usage before.