不能在SubCut中使用modifyBindings

时间:2012-11-09 12:56:44

标签: scala subcut

modifyBindings会按照承诺,此测试应该通过:

import org.scalatest.FunSuite
import org.scala_tools.subcut.inject.NewBindingModule
import org.scala_tools.subcut.inject.BindingModule
import org.scala_tools.subcut.inject.Injectable
import org.junit.Test

case class Resource1(name: String)

class ResourceClient(implicit val bindingModule: BindingModule) extends Injectable {
    val res1 = inject[Resource1]
}

object Config1 extends NewBindingModule(module => {
    module.bind[Resource1] toSingle new Resource1("name1")
})

class TestModifyBindings extends FunSuite {

@Test
def test1() {
    implicit val bindingModule = Config1
    val rc = new ResourceClient
    assert(rc.res1.name == "name1")
    Config1.modifyBindings { module =>
        module.bind[Resource1] toSingle new Resource1("name2")
        val rc2 = new ResourceClient //not sure if rc can be used here
        assert(rc2.res1.name == "name2") //FAILS
    }
}

}

关于我错过的任何想法?

1 个答案:

答案 0 :(得分:1)

如果你做

    Config1.modifyBindings { module =>

进入

    Config1.modifyBindings { implicit module =>

能解决这个问题吗?