0

クラスをテストしようとしています

@Singleton
class Foo @Inject()(bar: Bar)(implicit ec: ExecutionContext) {
  def doSomething = bar.doSomethingInBar
}

class Bar {
  def doSomethingInBar = true
}

Specification下記のクラスを通して

class FooTest @Inject()(foo: Foo) extends Specification {
  "foo" should {
    "bar" in {
      foo.doSomething mustEqual (true)
    }
  }
}

これを実行すると、次のエラーが表示されます

Can't find a constructor for class Foo

ここに記載されている解決策に従いました

そして定義されたInjector

object Inject {
  lazy val injector = Guice.createInjector()

  def apply[T <: AnyRef](implicit m: ClassTag[T]): T =
    injector.getInstance(m.runtimeClass).asInstanceOf[T]
}

そしてlazy val foo: Foo = Inject[Foo]私の仕様クラスの中に。コンストラクターの初期化の問題は解決しますが、現在このエラーが発生しています。

[error]   ! check the calculate assets function
[error]    Guice configuration errors:
[error]    
[error]    1) No implementation for scala.concurrent.ExecutionContext was bound.
[error]      while locating scala.concurrent.ExecutionContext
4

1 に答える 1

0

ExecutionEnvコードを機能させるには、暗黙的なものを提供する必要があります

@RunWith(classOf[JUnitRunner])
class FooTest(implicit ee: ExecutionEnv)  extends Specification {
  "foo" should {
    "bar" in {
      foo.doSomething mustEqual (true)
    }
  }
}

コードのどこかで、foo コンストラクトを初期化する必要があり、それをコンストラクターを介して渡すことができます。これが依存性注入のポイントです。

于 2016-12-14T05:27:41.057 に答える