Combining FakeRouterModule and ScaldiApplicationBuilder.withScaldiInj

56 views
Skip to first unread message

Ulrik Lejon

unread,
Jul 10, 2015, 5:13:07 AM7/10/15
to sca...@googlegroups.com
Hi

I need to create a test where I can inject stuff and also set up some fake routes (to fake an external web service).

I tried to do it like this:

"My service" should {

    val fakeRotes = FakeRouterModule {
            case ("POST", someUrl) =>
                Action { implicit request =>
                    BadRequest("Ops")
                }
    }

    "Do some stuff" in new TestContext {
        ScaldiApplicationBuilder.withScaldiInj(configuration = Configuration.from(DefaultConf.appCfg), modules = Seq(fakeRotes)) { implicit inj =>
               .....
        }
    }
}


However, I'm getting this when running the test: Connection refused: localhost/127.0.0.1:19001 (NettyConnectListener.java:128)
Is this suppose to work?

Cheers,
Ulrik

Alexandre Bedrytski

unread,
Jul 11, 2015, 4:26:31 AM7/11/15
to sca...@googlegroups.com
Sadly, I don't know why exactly this code gives Netty error, my wild guess would be that 'withScaldiInj' should surround The spec and not the other way around (you are running tests with sbt, aren't you?)

I've spent quite some time (not without Scaldi's creator help) to set up tests in my project. 
Here you may find the parent class of all specs in my project that automatically surrounds children's specs with application initialisations.
And here you may find an example of a spec that needs injector to be run.

Hope this will help, 
Alex

Ulrik Lejon

unread,
Jul 11, 2015, 4:54:28 AM7/11/15
to sca...@googlegroups.com
Thanx for sharing your specs, I will have a look at those.

Yes I'm running the tests with sbt. The problem is not that the spec is surrounding the 'withScaldiInj' block (it works in my other tests).
What makes this test "special" is that I need to run a server (TestServer), and not just an application. I also need to have access to the scaldi injector to be able to inject stuff in my test.

Cheers

Oleg Ilyenko

unread,
Jul 11, 2015, 10:24:25 AM7/11/15
to sca...@googlegroups.com, ulrik...@gmail.com
Hi Ulrik,

Since you need to access the `TestServer` I can recommend you to look at this example test:


It starts the server with fake routes. You also mentioned, that you need an injector in the test, because you need to inject some bindings. It's should be pretty easy achieve:

val fakeRotes = FakeRouterModule {
  case ("GET", "/some-url") => Action {
    Results.Ok("everything is fine")
  }
}

implicit val injector = new ScaldiApplicationBuilder(modules = Seq(fakeRotes)).buildInj()
import scaldi.Injectable._

running(TestServer(3333, inject [Application]), HTMLUNIT) { browser =>
  val someService = inject [SomeService]


  browser.pageSource must contain("everything is fine")

Hope this helps.

Cheers,
Oleg


On Friday, July 10, 2015 at 11:13:07 AM UTC+2, Ulrik Lejon wrote:

Ulrik Lejon

unread,
Jul 11, 2015, 11:02:32 AM7/11/15
to sca...@googlegroups.com, ulrik...@gmail.com
Hey Oleg,

Thanx! It works great, as usual! :)

Cheers,
Ulrik
Reply all
Reply to author
Forward
0 new messages