简体   繁体   中英

ScalaTest PlayFramework

Trying to do some TDD while learning the playframework:

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:

def fourOhFour() = NotFound(s"Oops, content could not be found")

yet the assertion service.find("fr") must be Result.NotFound won't compile. 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. Actually I use MVC, but a Service + repository-layer as well:

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

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:

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

Maybe you're comparing a Result with Result.Status type?

Can you use a 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM