[英]ScalaTest PlayFramework
Trying to do some TDD while learning the playframework: 在学习游戏框架时尝试做一些TDD:
class ContentFetching extends PlaySpec with BeforeAndAfter with MockFactory
{
private val service = ContentService
private val languages = List(Language(Some(1), "en", "English", "Placeholder"),
Language(Some(2), "de", "Deutsch", "Platzhalter")
)
"find" must
{
"fail if languageCode is invalid" in
{
val fakeRepository = mock[LanguageRepositoryTrait]
(fakeRepository.get _).expects().returning(languages)
fakeRepository.get must have length 3
service.find("fr") must be Result.NotFound
}
}
}
ContentService would call: ContentService将调用:
def fourOhFour() = NotFound(s"Oops, content could not be found")
yet the assertion service.find("fr") must be Result.NotFound
won't compile. 但断言
service.find("fr") must be Result.NotFound
不会编译。 Why is that and how to test this then? 为什么会这样,然后如何测试呢? Here is the whole contentService (as requested) Sadly it extends Controller currently, because I found no other way to return an Action.
这是整个contentService(根据要求)可悲的是,它目前正在扩展Controller,因为我发现没有其他方法可以返回Action。 Actually I use MVC, but a Service + repository-layer as well:
实际上,我使用MVC,但也使用Service +存储库层:
class ContentServiceComponent(languageRepository: LanguageRepositoryTrait, nodeRepository: NodeRepositoryTrait) extends Controller
{
def find(language: String) = Action
{
languageRepository.get().map(l => l.code).contains(language) match
{
case true => Ok(s"Homepage for $language")
case false => fourOhFour()
}
}
def fourOhFour() = NotFound(s"Oops, content could not be found")
}
object ContentService extends ContentServiceComponent(LanguageRepository, NodeRepository)
find
instead returns Action
could return Option
find
代替返回Action
可以返回Option
def find(language: String): Option[String] =
{
languageRepository.get().map(l => l.code).contains(language) match
{
case true => Some(s"Homepage for $language")
case _ => None
}
}
So you cound handle it at Controller as follow: 因此,您可以按照以下说明在Controller处处理该问题:
def index(language:String) = Action{
ContentService.find(language) match{
case Some(content) => Ok(content)
case _ => NotFound(s"Oops, content could not be found")
}
} }
and you could do test case something like this: 你可以做这样的测试用例:
class ContentFetching extends PlaySpec with BeforeAndAfter with MockFactory
{
val languages = List(Language(Some(1), "en", "English",Placeholder"),Language(Some(2), "de", "Deutsch", "Platzhalter")
)
val languagesRepositoryMockHere = new LanguageRepositoryTrait{
override def get = languages //I don't know the implementations
}
"find" must
{
"fail if languageCode is invalid" in
{
private val service = new ContentServiceComponent(languagesRepositoryMockHere,nodeRepositoryMockHere)
service.find("fr") shouldBe None
}
}
}
This was not tested but could help for reference. 这未经测试,但可以帮助参考。
Cheers. 干杯。
service.find("fr") from the Play controller returns a Result 播放控制器中的service.find(“ fr”)返回结果
Maybe you're comparing a Result with Result.Status type? 也许您正在将Result与Result.Status类型进行比较?
Can you use a play.api.helper
? 您可以使用
play.api.helper
吗?
status(of: Future[Result]) = Await.result(of, timeout.duration).header.status
Instead of 代替
service.find("fr") must be Result.NotFound
Try something like this? 尝试这样的事情?
status(service.find("fr")) must be NOT_FOUND
NOT_FOUND
is a play.api.http.Status
value and just maps to the standard HTTP status code 404 NOT_FOUND
是play.api.http.Status
值,仅映射到标准HTTP状态代码404
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.