2015-06-12 56 views
2

我遇到了依賴關係問題,顯然在測試之間出血,導致大多數測試失敗。在每種情況下,調試都會顯示在測試類中創建的第一個應用程序用於所有測試,這會導致失敗。Play中的Specs2/Guice問題2.4.0功能測試

我試過添加isolatedsequential,這沒有效果。

我在做一些非常愚蠢或微妙愚蠢的事情嗎?

例如,這裏的SubjectNotPresentTest.scala

class SubjectNotPresentTest extends AbstractViewTest { 

    "show constrained content when subject is not present" in new WithApplication(testApp(handler())) { 
    val html = subjectNotPresentContent(FakeRequest()) 

    private val content: String = Helpers.contentAsString(html) 
    content must contain("This is before the constraint.") 
    content must contain("This is protected by the constraint.") 
    content must contain("This is after the constraint.") 
    } 

    "hide constrained content when subject is present" in new WithApplication(testApp(handler(subject = Some(user())))) { 
    val user = new User("foo", Scala.asJava(List.empty), Scala.asJava(List.empty)) 
    val html = subjectNotPresentContent(FakeRequest()) 

    private val content: String = Helpers.contentAsString(html) 
    content must contain("This is before the constraint.") 
    content must not contain("This is protected by the constraint.") 
    content must contain("This is after the constraint.") 
    } 
} 

GuiceApplicationBuilder在parent class使用用於創建用於測試的應用程序。

val app = new GuiceApplicationBuilder() 
      .bindings(new DeadboltModule()) 
      .bindings(bind[HandlerCache].toInstance(LightweightHandlerCache(handler))) 
      .overrides(bind[CacheApi].to[FakeCache]) 
      .in(Mode.Test) 
      .build() 

您可以在https://travis-ci.org/schaloner/deadbolt-2-scala/builds/66369307#L805

看到失敗的例子所有的測試可以在https://github.com/schaloner/deadbolt-2-scala/tree/master/code/test/be/objectify/deadbolt/scala/views找到

感謝, 史蒂夫

回答

1

它看起來像問題時引起的電流Play應用程序在有多個應用程序的測試環境中靜態引用 - 即使它們在邏輯上是分開的。

由於組件不能被注入(據我所知)到模板中,我創建了一個helper object,它使用Play.current.injector來定義一對val s。

val viewSupport: ViewSupport = Play.current.injector.instanceOf[ViewSupport] 
    val handlers: HandlerCache = Play.current.injector.instanceOf[HandlerCache] 

(這也是不可能的,TTBOMK,要注入的對象,否則我可能只是注入組件插入的對象,大家都回家了)。

一個更好的方法是揭示隱含的要求。

object ViewAccessPoint { 

    private[deadbolt] val viewStuff = Application.instanceCache[ViewSupport] 
    private[deadbolt] val handlerStuff = Application.instanceCache[HandlerCache] 

    object Implicits { 
     implicit def viewSupport(implicit application: Application): ViewSupport = viewStuff(application) 
     implicit def handlerCache(implicit application: Application): HandlerCache = handlerStuff(application) 
    } 
} 

在視圖中,導入implicits,你很好去。

@import be.objectify.deadbolt.scala.DeadboltHandler 
@import be.objectify.deadbolt.scala.ViewAccessPoint.Implicits._ 
@import play.api.Play.current 
@(handler: DeadboltHandler = handlerCache(current).apply(), name: String, meta: String = null, timeout: Function0[Long] = viewSupport.defaultTimeout)(body: => play.twirl.api.Html)(implicit request: Request[Any]) 

@if(viewSupport.dynamic(name, meta, handler, timeout(), request)) { 
@body 
}