我有这样的设置:
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)
这表明没有应用程序存在....?!
我的设置有问题吗?
答案 0 :(得分: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的下一个版本应该有an even easier version用于您要执行的操作:
class MySpec extends Specification with ApplicationExample {
// only override if necessary
implicit override val app =
FakeApplication(additionalConfiguration = Map("foo" -> "bar"))
"something" should {
...
}
}