2016-08-04 64 views
0

基本應用程序體系結構 App architecture 查看Official 2.5 WS Test Documentation,它只會談到獲取同步的響應。如何在Play中測試防火和忘記WSClient請求

object GitHubClientSpec extends Specification with NoTimeConversions { 

    "GitHubClient" should { 
    "get all repositories" in { 

     Server.withRouter() { 
     case GET(p"/repositories") => Action { 
      Results.Ok(Json.arr(Json.obj("full_name" -> "octocat/Hello-World"))) 
     } 
     } { implicit port => 
     WsTestClient.withClient { client => 
      val result = Await.result(
      new GitHubClient(client, "").repositories(), 10.seconds) 
      result must_== Seq("octocat/Hello-World") 
     } 
     } 
    } 
    } 
} 

,如果我想測試會發生什麼步驟在我的建築4,5,6?我可以使用WSTestClient嗎?文件似乎稀疏它

這裏是我想通過我的返工架構測試

def createGraphvizDotStringAndReturnImgurLink = Action.async{ implicit request => 
    import SlashCommandIn._ 

    slackForm.bindFromRequest.fold(
     formWithErrors => { 
      Logger.warn(s"Incorrect Form Format: ${request.body.asText.getOrElse("<No Body>")}}") 
      Future{Ok(Json.toJson(SlackPrivateUserResponse(BAD_FORM_DATA_MSG)))} 
     }, 

     goodValidatedSlackRequest => { 
      if(goodValidatedSlackRequest.token.contentEquals(SLACK_EXPECTED_TOKEN)) { 
       Logger.debug("Validation Succeeded") 
       _doImageCreationAndGetImgurLink(goodValidatedSlackRequest) // Steps, 4, 5, and 6 
       Future{Ok(Json.toJson(SlackPrivateUserResponse(PROCESSING_MSG + "\n>>>" + 
        goodValidatedSlackRequest.text)))} 

      }else { 
       Future{Ok(Json.toJson(SlackPrivateUserResponse(BAD_TOKEN_MSG)))} 
      } 
     } 
    ) 
} 

回答

0

解決了這個相關的代碼。

真的,我試圖使用單元測試方法與集成測試。

如果你想測試一個需要WSClient的控制器,你應該進行單元測試並在實例化控制器時注入它。

基本上,您不能在嘗試實現黑盒測試時進行白盒測試。

參見ScalaTest和測試控制器

import scala.concurrent.Future 

import org.scalatestplus.play._ 

import play.api.mvc._ 
import play.api.test._ 
import play.api.test.Helpers._ 

class ExampleControllerSpec extends PlaySpec with Results { 

    "Example Page#index" should { 
    "should be valid" in { 
     val controller = new ExampleController() 
     val result: Future[Result] = controller.index().apply(FakeRequest()) 
     val bodyText: String = contentAsString(result) 
     bodyText mustBe "ok" 
    } 
    } 
} 

源播放文檔:https://www.playframework.com/documentation/2.5.x/ScalaTestingWithScalaTest