2016-08-04 112 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