§使用 Guice 進行測試
如果您使用 Guice 進行 依賴注入,則您可以直接設定元件和應用程式如何為測試建立。這包括新增額外繫結或覆寫現有繫結。
§GuiceApplicationBuilder
GuiceApplicationBuilder 提供一個建構器 API,用於設定依賴注入和建立 應用程式。
§環境
環境,或環境的部分(例如應用程式的根路徑、模式或類別載入器),可以指定。設定的環境將用於載入應用程式設定檔,它將用於載入模組,並在從 Play 模組衍生繫結時傳遞,並且可以注入到其他元件中。
import play.api.inject.guice.GuiceApplicationBuilder
val application = new GuiceApplicationBuilder()
.load(
new play.api.inject.BuiltinModule,
new play.api.i18n.I18nModule,
new play.api.mvc.CookiesModule
.in(Environment(new File("path/to/app"), classLoader, Mode.Test))
.build()
val application = new GuiceApplicationBuilder()
.load(
new play.api.inject.BuiltinModule,
new play.api.i18n.I18nModule,
new play.api.mvc.CookiesModule
.in(new File("path/to/app"))
.in(Mode.Test)
.in(classLoader)
.build()
§設定檔
可以新增額外的設定檔。此設定檔將永遠新增到自動為應用程式載入的設定檔中。當使用現有金鑰時,將優先使用新的設定檔。
val application = new GuiceApplicationBuilder()
.configure(Configuration("a" -> 1))
.configure(Map("b" -> 2, "c" -> "three"))
.configure("d" -> 4, "e" -> "five")
.build()
也可以覆寫從應用程式環境自動載入設定檔。這將完全取代應用程式設定檔。例如
val application = new GuiceApplicationBuilder()
.loadConfig(env => Configuration.load(env))
.build()
§繫結和模組
用於依賴注入的繫結完全可設定。建構器方法支援 Play 模組和繫結,以及 Guice 模組。
§額外繫結
可透過 Play 模組、Play 繫結或 Guice 模組新增其他繫結
import play.api.inject.bind
val injector = new GuiceApplicationBuilder()
.bindings(new ComponentModule)
.bindings(bind[Component].to[DefaultComponent])
.injector()
§覆寫繫結
繫結可使用 Play 繫結或提供繫結的模組來覆寫。例如
val application = new GuiceApplicationBuilder()
.overrides(bind[Component].to[MockComponent])
.build()
§停用模組
任何已載入的模組都可以按類別名稱停用
val injector = new GuiceApplicationBuilder()
.disable[ComponentModule]
.injector()
§已載入的模組
模組會根據 play.modules.enabled
設定檔自動從類別路徑載入。此模組的預設載入可以覆寫。例如
val injector = new GuiceApplicationBuilder()
.load(
new play.api.inject.BuiltinModule,
new play.api.i18n.I18nModule,
new play.api.mvc.CookiesModule,
bind[Component].to[DefaultComponent]
)
.injector()
§GuiceInjectorBuilder
GuiceInjectorBuilder 提供一個建構器 API,用於更普遍地設定 Guice 相依性注入。此建構器不會像 GuiceApplicationBuilder
一樣自動從環境載入設定檔或模組,但會提供一個完全乾淨的狀態,用於新增設定檔和繫結。兩個建構器的共用介面可以在 GuiceBuilder 中找到。會建立一個 Play Injector。以下是使用注入器建構器實例化元件的範例
import play.api.inject.guice.GuiceInjectorBuilder
import play.api.inject.bind
val injector = new GuiceInjectorBuilder()
.configure("key" -> "value")
.bindings(new ComponentModule)
.overrides(bind[Component].to[MockComponent])
.injector()
val component = injector.instanceOf[Component]
§在功能測試中覆寫繫結
以下是使用模擬元件取代元件進行測試的完整範例。我們從一個元件開始,它有一個預設實作和一個用於測試的模擬實作
trait Component {
def hello: String
}
class DefaultComponent extends Component {
def hello = "default"
}
class MockComponent extends Component {
def hello = "mock"
}
此元件會使用模組自動載入
import play.api.inject.Binding
import play.api.inject.Module
import play.api.Configuration
import play.api.Environment
class ComponentModule extends Module {
def bindings(env: Environment, conf: Configuration): Seq[Binding[_]] = Seq(
bind[Component].to[DefaultComponent]
)
}
此元件會在控制器中使用
import javax.inject.Inject
import play.api.mvc._
class Application @Inject() (component: Component, cc: ControllerComponents) extends AbstractController(cc) {
def index = Action {
Ok(component.hello)
}
}
若要建立一個供功能測試使用的 Application
,我們可以簡單地覆寫元件的繫結
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.inject.bind
val application = new GuiceApplicationBuilder()
.overrides(bind[Component].to[MockComponent])
.build()
已建立的應用程式可用於 Specs2 和 ScalaTest 的功能測試輔助程式。
下一步:使用編譯時期依賴注入進行測試
在此文件當中發現錯誤?此頁面的原始程式碼可在此處找到 here。在閱讀 文件指南 後,請隨時貢獻一個 pull request。有問題或建議要分享嗎?請前往 我們的社群論壇 與社群展開對話。