2

I'm trying to run a test with scaldi and specs2. In the test I need to override a StringManipulator function that uses an injected ProxyManipulator. The ProxyManipulator takes a string and returns its upper case in a Future. The replacement manipulator in the test returns a Future("Test Message").

Here is the StringManipulator class where the injection occurs:

class StringManipulator {

  def manip (str : String) (implicit inj: Injector) : String = {
    val prox = inject[ProxyManipulator]
    Await.result(prox.manipulate(str), 1 second)
  }
}

I'm using a package.object that contains the implicit injector:

import modules.MyModule

package object controllers {
  implicit val appModule = new MyModule
}

And here is the specs2 test with the new binding:

@RunWith(classOf[JUnitRunner])
class StringManipScaldiSpec extends Specification {

  class TestModule extends Module {

    bind [ProxyManipulator] to new ProxyManipulator {
      override def manipulate(name: String) = Future("Test Message")
    }
  }

  "Application" should {
    "do something" in {

      val myTestModule = new TestModule

      val str = "my string"

      val stringMan = new StringManipulator() //(myTestModule)

      stringMan.manip(str)(myTestModule) === "Test Message"

    }
  }
}

The problem is that when the test runs the class StringManipulator is still using the original Proxy Manipulator instead of the one passed in the TestModule. Any ideas?

Yossi Chen
  • 226
  • 3
  • 11
  • 1
    Did you try to make your `myTestModule` implicit ? – Mironor Jan 07 '16 at 21:54
  • @mironor yup, already tried that, still no success. – Yossi Chen Jan 10 '16 at 14:42
  • 1
    Take a look at `foreach` function here https://github.com/Mironor/livrarium/blob/master/test/helpers/LivrariumSpecification.scala this also implies that in each of your tests you should add implicit parameter `inj` like here https://github.com/Mironor/livrarium/blob/master/test/daos/BookDAOSpec.scala . And yes, it looks quite cumbersome, I hope that Oleg (Scaldi's author) passes by to give a more elegant solution without so much boilerplate code. – Mironor Jan 11 '16 at 04:59

0 Answers0