1

我试图弄清楚如何使用 specs2 在 Play 框架中测试多个 URL,但如果不启动多个服务器,我似乎无法找出正确的方法。

import org.specs2.mutable._

import play.api.test._
import play.api.test.Helpers._
import play.api.libs.ws.WS

class testServer extends Specification {

  running(TestServer(9000)) {
    "Server starts" in {
      await(WS.url("http://localhost:9000").get).status must equalTo(OK)
    }
    "Who" in {
      await(WS.url("http://localhost:9000/url1").get).status must equalTo(OK)
    }
    "How" in {
      await(WS.url("http://localhost:9000/url2").get).status must equalTo(OK)
    }
    "Why" in {
      await(WS.url("http://localhost:9000/url3").get).status must equalTo(OK)
    }
    "Test4" in {
      await(WS.url("http://localhost:9000/url4").get).status must equalTo(OK)
    }
    "Test" in {
      await(WS.url("http://localhost:9000/url5").get).status must equalTo(OK)
    }
    "Help" in {
      await(WS.url("http://localhost:9000/url6").get).status must equalTo(OK)
    }
    "Bogus" in {
      await(WS.url("http://localhost:9000/url7").get).status must equalTo(OK)
    }
  }
}
4

1 回答 1

2

请参阅此问题的答案。您需要为每个示例创建一个新的上下文。例如你可以写:

import org.specs2._
import execute._
import specification.AroundExample

class testServer extends Specification with AroundExample {
  def around[R <% Result](r: =>R) = 
    running(TestServer(9000))(r)

   "Server starts" in {
     await(WS.url("http://localhost:9000").get).status must equalTo(OK)
   }
   ...
}
于 2012-11-25T07:55:46.133 回答