2016-12-15 1 views
0

내 플레이 응용 프로그램이이 같은이있는 경우 :My Play Application의 생성자가 인수를 취합니다. Spec Test에서 조롱 한 인수를 부여하려면 어떻게해야합니까?

class Foo() extends Bar {} 

class Application @Inject (f: Foo) extends Controller { 
    def index = Action { OK("Hi, Foo App") } 
} 

가 어떻게 MockedFoo 클래스를 받아 내 사양 테스트를 변경하려면 어떻게해야합니까? 도움을

@RunWith(classOf[JUnitRunner]) 

class MockedFoo() extends Bar {} 

class ApplicationTest(implicit ee: ExecutionEnv) extends Specification { 

    "Sending a GET request to index " should { 

    "Respond with OK " in new WithApplication { //######## Inject MockedFoo 
     val response = route(app, FakeRequest(GET, "/")).get 
     status(response) mustEqual OK 
    } 
    } 
} 

감사 : 내 자신의 요지를

답변

0

복사 : https://gist.github.com/rethab/01fde763d10f29273d43

첫째, 편의를 위해 도우미 클래스 생성 :

class WithFancyApp(lang: Lang = Lang.defaultLang, 
        overrideModules: Seq[GuiceableModule] = Seq()) extends 
    WithApplication(
    app = 
     new GuiceApplicationBuilder() 
     .in(Environment(new File("."), getClass.getClassLoader, Mode.Test)) 
     .loadConfig(env => Configuration.load(env)) 
     .overrides(overrideModules:_*) 
     .bindings() 
     .build 
) { 

    implicit def messages: Messages = Messages(lang, app.injector.instanceOf[MessagesApi]) 

} 

사용법 :

"use the overridden bindigs" in new WithFancyApp(
     overrideModules = Seq(bind[MyInterface].to[MyImplementation]) 
) { 
    // test stuff with all regular bindings plus the ones from above 
} 
+0

오히려 GuiceModule을하지 마십시오. 너무 간단하게 들리는 것 같아요. 다른 바 구현 (MockedFoo)을 Test의 Application 생성자에 전달해야합니다. Guice 모듈 만이 유일한 방법 일 수는 없습니다! – dlite922

+0

'bind [MyInterface] .to [MyImplementation]'에서'GuiceableModule'로 암시 적으로 변환되므로 아무 것도 할 필요가 없습니다. – rethab

+0

수입 라인은 어디에 있습니까? – dlite922

관련 문제