4

我有这样的设置:

class PriceAwareSpec extends Specification  {

 sequential

 running(FakeApplication()) {

 val price = "CHF 50.00"
 val priceAsHtml: Html = Html(price)

 val context: Context = ContextAccessUtil.getContext
 val stateAccess = ServiceFactoryUtil.getService(context, classOf[StateAccess])

 "price aware template" should {

   "per default" in {

    stateAccess.store(StateKeys.HIDE_NETTO_KEY, java.lang.Boolean.FALSE)

     "show netto" in {
       val html = views.html.price.priceAware(priceAsHtml, true)(request)
       contentAsString(html) must contain(price)
     }

     "show brutto" in {
       val html = views.html.price.priceAware(priceAsHtml, false)(request)
       contentAsString(html) must contain(price)
     }
   }
 }
}

随着调用

ContextAccessUtil.getContext

有一个访问播放缓存的权限

play.cache.Cache.get("foo")

它确实尝试从当前应用程序中获取信息(=FakeApplication() ?)

这会导致 NullPointerException:

[error]       NullPointerException: null (Cache.scala:-1)
[error] play.api.cache.Cache$.get(Cache.scala:57)
[error] play.api.cache.Cache.get(Cache.scala)
[error] play.cache.Cache.get(Cache.java:16)

这表明没有应用程序存在......?!

我的设置有问题吗?

4

1 回答 1

3

正如Play 2.0 wiki中提到的,必须为每个示例创建上下文:

"Computer model" should {

  "be retrieved by id" in new WithApplication {
    val Some(macintosh) = Computer.findById(21)

    macintosh.name must equalTo("Macintosh")
    macintosh.introduced must beSome.which(dateIs(_, "1984-01-24"))  
 }

 // or, if you want to be more specific about your configuration
 "be retrieved by id" in new 
    WithApplication(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
    ...
 }

}

这确保每个示例都在FakeApplication. 另一方面,您在代码中所做的是围绕示例的创建,FakeApplication对它们的执行没有影响。请注意,Play 2.0 的下一个版本应该有一个更简单的版本来满足您的要求:

class MySpec extends Specification with ApplicationExample {

  // only override if necessary
  implicit override val app = 
    FakeApplication(additionalConfiguration = Map("foo" -> "bar"))

  "something" should {
    ...
  }

}
于 2012-11-22T10:14:55.043 回答