1

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
    }
}

}

关于我错过了什么的任何想法?

4

1 回答 1

1

If you make

    Config1.modifyBindings { module =>

into

    Config1.modifyBindings { implicit module =>

does that fix it?

于 2013-10-24T05:09:02.033 回答