当前位置: 首页>>代码示例>>Scala>>正文


Scala PlaySpec类代码示例

本文整理汇总了Scala中org.scalatestplus.play.PlaySpec的典型用法代码示例。如果您正苦于以下问题:Scala PlaySpec类的具体用法?Scala PlaySpec怎么用?Scala PlaySpec使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了PlaySpec类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: CountriesDestroySpec

//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec

import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._

import play.api.libs.json._

import test.helpers.{DatabaseCleaner, DatabaseInserter}

import play.api.Play

import utils.TimeUtil.now

class CountriesDestroySpec extends PlaySpec with BeforeAndAfterAll {
  val app = new GuiceApplicationBuilder().build

  override def beforeAll() {
    Play.maybeApplication match {
      case Some(app) => ()
      case _ => Play.start(app)
    }

    DatabaseCleaner.clean(List("Countries"))
    DatabaseInserter.insert("Users", 12, List("[email protected]", "John", "Doe", "password", "token", now.toString, "User", "1", "999999", "2016-01-01"))
    DatabaseInserter.insert("Users", 13, List("[email protected]", "Johnny", "Doe", "password", "admin_token", now.toString, "Admin", "1", "999999", "2016-01-01"))
    DatabaseInserter.insert("Countries", 11, List("Denmark", "DEN", "1", "2016-10-10"))
  }

  override def afterAll() {
    DatabaseCleaner.clean(List("Countries"))
  }

  "deleting countries" should {
    "returns 404 if no such record" in {
      val request = FakeRequest(DELETE, "/countries/200").withHeaders("Authorization" -> "admin_token")
      val destroy = route(app, request).get

      status(destroy) mustBe NOT_FOUND
    }

    "returns 204 if successfully deleted and user is Admin" in {
      val request = FakeRequest(DELETE, "/countries/11").withHeaders("Authorization" -> "admin_token")
      val destroy = route(app, request).get

      status(destroy) mustBe NO_CONTENT
    }

    "returns 403 if user is not Admin" in {
      val request = FakeRequest(DELETE, "/countries/11").withHeaders("Authorization" -> "token")
      val destroy = route(app, request).get

      status(destroy) mustBe FORBIDDEN
    }
  }
} 
开发者ID:dsounded,项目名称:money_exchanger,代码行数:58,代码来源:CountriesDestroySpec.scala

示例2: myPublicAddress

//设置package包名称以及导入依赖的类
package org.zalando.hutmann.spec

import org.scalatest.concurrent.PatienceConfiguration.Interval
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.{ Seconds, Span }
import org.scalatestplus.play.{ PlaySpec, PortNumber, WsScalaTestClient }
import play.api.Application
import play.api.http.{ HeaderNames, MimeTypes }
import play.api.libs.ws.{ WSClient, WSResponse }

trait PlayUnitSpec extends PlaySpec with ScalaFutures with WsScalaTestClient {
  def myPublicAddress(): String
  implicit val portNumber: PortNumber

  def callWs(testPaymentGatewayURL: String)(implicit app: Application): WSResponse = {
    implicit val wSClient = app.injector.instanceOf[WSClient]
    val callbackURL = s"http://${myPublicAddress()}/callback"
    whenReady(
      wsUrl(testPaymentGatewayURL)
        .withQueryStringParameters("callbackURL" -> callbackURL)
        .withHttpHeaders(HeaderNames.ACCEPT -> MimeTypes.TEXT)
        .get(),
      Interval(Span(10, Seconds))
    ) { result =>
        result
      }
  }
} 
开发者ID:zalando-incubator,项目名称:hutmann,代码行数:29,代码来源:PlayUnitSpec.scala

示例3: PlayWithFoodWithTestComponents

//设置package包名称以及导入依赖的类
package testhelpers

import config.ExampleComponents
import org.scalatest.BeforeAndAfterEach
import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.components.OneServerPerTestWithComponents
import play.api.db.evolutions.Evolutions
import play.api.http.Status
import play.api.libs.ws.WSClient
import play.api.test.{DefaultAwaitTimeout, FutureAwaits, Helpers, TestServer}
import play.api.{Application, Configuration}
import slick.dbio.DBIO

trait PlayWithFoodWithServerBaseTest extends PlaySpec
  with OneServerPerTestWithComponents
  with DefaultFutureDuration
  with DefaultExecutionContext
  with Status
  with DefaultAwaitTimeout
  with FutureAwaits
  with BeforeAndAfterEach {

  class PlayWithFoodWithTestComponents extends ExampleComponents(context) {

    override def configuration: Configuration = {
      val testConfig = Configuration.from(TestUtils.config)
      val config = super.configuration
      val testConfigMerged = config ++ testConfig
      config.toString
      testConfigMerged
    }

    implicit lazy val testWsClient: WSClient = wsClient
  }

  override def components: PlayWithFoodWithTestComponents = new PlayWithFoodWithTestComponents

  private def runServerAndCleanUpInMemDb(c: PlayWithFoodWithTestComponents) = {
    runServerAndExecute(c.application) {
      val dbapi = c.dbApi
      Evolutions.cleanupEvolutions(dbapi.database("default"))
    }
  }

  override protected def afterEach(): Unit = {
    runServerAndCleanUpInMemDb(components)
  }

  protected def runServerAndExecute[T](app: Application)(blockWithComponents: => T): T = {
    Helpers.running(TestServer(port, app))(blockWithComponents)
  }

  def runAndAwaitResult[T](action: DBIO[T])(implicit components: ExampleComponents): T = {
    TestUtils.runAndAwaitResult(action)(components.actionRunner, duration)
  }

} 
开发者ID:Dasiu,项目名称:play-framework-scala-example-project,代码行数:58,代码来源:PlayWithFoodWithServerBaseTest.scala

示例4: MongoSpec

//设置package包名称以及导入依赖的类
import com.fasterxml.jackson.annotation.JsonValue
import org.scalatestplus.play.PlaySpec
import play.api.test._
import play.modules.reactivemongo._
import services._
import connectors._
import play.api.libs.json.{JsValue, Json}
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.bson
import reactivemongo.bson.{BSONDocument, BSONObjectID}
import play.api.test.Helpers._

class MongoSpec extends PlaySpec {

  val mongo = new MongoExecutor(new MyMongoConnection)

  "mongo" should {
    val document = BSONDocument(
      "_id" -> BSONObjectID.parse("5951aa22c8e144a1c7e2b9dc").get,
      "name" -> "mongoTest",
      "requests" -> 0,
      "userErrors" -> 0
    )

    "return a BSON document" in {
      val result = await(mongo.getController("mongoTest"))
      result mustBe Some(document)
      result match {
        case Some(document) => document.getAs[Int]("requests").map(x => x mustBe 0)
        case None => throw fail("No document found")
      }
    }
  }
} 
开发者ID:ZDevelop94,项目名称:ZolvedIT,代码行数:35,代码来源:MongoSpec.scala

示例5: HomePageControllerSpec

//设置package包名称以及导入依赖的类
package controllers

import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import play.api.test.FakeRequest
import play.api.test.Helpers._



class HomePageControllerSpec extends PlaySpec with OneAppPerSuite {

  //TODO: Need to define TimeGreetingService

  object TestHomeControllerTest extends HomeController {
    override def greeter = ???
  }
  val controller = TestHomeControllerTest

  "HomeController" should {
    "not return 404" when {
      "I go to the route /landing" in {
        val result = route(app, FakeRequest(GET, "/landing"))
        status(result.get) must not be NOT_FOUND
      }
    }

    "render the correct page with the expected text" when {
      "I navigate to the homepage" in {
        val result = controller.landing("Good morning Helen").apply(FakeRequest(GET, "/landing"))

        status(result) mustBe OK
        contentAsString(result) must include ("Good morning Helen")
        //arrange
        //action
        //assert
      }
      "I go to the homepage after lunch" in {
        val result = controller.landing("Good afternoon Helen").apply(FakeRequest(GET, "/landing"))

        contentAsString(result) must include ("Good afternoon Helen")
      }
    }
  }
} 
开发者ID:Hejocaso,项目名称:LunchApp,代码行数:44,代码来源:HomePageControllerSpec.scala

示例6: BreakfastAppControllerSpec

//设置package包名称以及导入依赖的类
package controllers

import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
import play.api.mvc.Result
import play.api.test.FakeRequest
import play.api.test.Helpers._

import scala.concurrent.Future

class BreakfastAppControllerSpec extends PlaySpec with OneAppPerSuite {
  "BreakfastAppController" should {
    "not return 404" when {
      "we try to hit the route /home" in {
        route(app, FakeRequest(GET, "/home")).map(status(_)) must not be Some(NOT_FOUND)
      }
    }
    "reneder a page" when {
      "we try to hit the route /home" in {
        var result: Option[Future[Result]] = route(app, FakeRequest(GET, "/home"))

        result.map(status(_)) mustBe Some(OK)

        val text: String = result.map(contentAsString(_)).get
        text must include ("Welcome to Play")
      }
    }
  }
} 
开发者ID:Hejocaso,项目名称:LunchApp,代码行数:29,代码来源:BreakfastAppControllerSpec.scala

示例7: SessionCreateSpec

//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec

import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._

import play.api.libs.json._

import test.helpers.{DatabaseCleaner, DatabaseInserter}

import play.api.Play

import com.github.t3hnar.bcrypt._

class SessionCreateSpec extends PlaySpec with BeforeAndAfterAll {
  val app = new GuiceApplicationBuilder().build

  override def beforeAll() {
    Play.maybeApplication match {
      case Some(app) => ()
      case _ => Play.start(app)
    }

    DatabaseCleaner.clean(List("Users"))

    val password = "password".bcrypt

    DatabaseInserter.insert("Users", 12, List("[email protected]", "John", "Doe", password, "token", "2016-01-01", "User", "1", "999999", "2016-01-01"))
  }

  override def afterAll() {
    DatabaseCleaner.clean(List("Users"))
  }

  "creates session if data is ok" in {
    val request = FakeRequest(POST, "/session").withJsonBody(Json.parse("""{ "email":"[email protected]", "password":"password" }"""))
    val create = route(app, request).get

    status(create) mustBe CREATED
    contentType(create) mustBe Some("application/json")
    contentAsString(create) must include("user")
    contentAsString(create) must include("authToken")
  }

  "responses with error if data is not ok" in {
    val request = FakeRequest(POST, "/session").withJsonBody(Json.parse("""{ "email":"[email protected]", "password":"bad_password" }"""))
    val create = route(app, request).get

    status(create) mustBe BAD_REQUEST
    contentType(create) mustBe Some("application/json")
    contentAsString(create) must include("error")
  }
} 
开发者ID:dsounded,项目名称:money_exchanger,代码行数:55,代码来源:SessionCreateSpec.scala

示例8: CountriesShowSpec

//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec

import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._

import play.api.libs.json._

import test.helpers.{DatabaseCleaner, DatabaseInserter}

import play.api.Play

import utils.TimeUtil.now

class CountriesShowSpec extends PlaySpec with BeforeAndAfterAll {
  val app = new GuiceApplicationBuilder().build

  override def beforeAll() {
    Play.maybeApplication match {
      case Some(app) => ()
      case _ => Play.start(app)
    }

    DatabaseCleaner.clean(List("Countries"))
    DatabaseInserter.insert("Users", 12, List("[email protected]", "John", "Doe", "password", "token", now.toString, "User", "1", "999999", "2016-01-01"))
    DatabaseInserter.insert("Countries", 88, List("Austria", "AU", "0", "2015-01-01"))
  }

  override def afterAll() {
    DatabaseCleaner.clean(List("Countries"))
  }

  "renders error" in {
    val request = FakeRequest(GET, "/countries/20").withHeaders("Authorization" -> "token")
    val show = route(app, request).get

    status(show) mustBe NOT_FOUND
    contentType(show) mustBe Some("application/json")
    contentAsString(show) must include("error")
    contentAsString(show) must include("not found")
  }

  "renders country" in {
    val request = FakeRequest(GET, "/countries/88").withHeaders("Authorization" -> "token")
    val show = route(app, request).get

    status(show) mustBe OK
    contentType(show) mustBe Some("application/json")
    contentAsString(show) must include("country")
  }
} 
开发者ID:dsounded,项目名称:money_exchanger,代码行数:53,代码来源:CountriesShowSpec.scala

示例9: CountriesIndexSpec

//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec

import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._

import play.api.libs.json._

import test.helpers.{DatabaseCleaner, DatabaseInserter}

import play.api.Play

import utils.TimeUtil.now

class CountriesIndexSpec extends PlaySpec with BeforeAndAfterAll {
  val app = new GuiceApplicationBuilder().build

  override def beforeAll() {
    Play.maybeApplication match {
      case Some(app) => ()
      case _ => Play.start(app)
    }

    DatabaseCleaner.clean(List("Countries"))
    DatabaseInserter.insert("Users", 12, List("[email protected]", "John", "Doe", "password", "token", now.toString, "User", "1", "999999", "2016-01-01"))
  }

  override def afterAll() {
    DatabaseCleaner.clean(List("Countries"))
  }

  "renders the forbidden for unauthorized response" in {
    val request = FakeRequest(GET, "/countries")
    val index = route(app, request).get

    status(index) mustBe FORBIDDEN
    contentType(index) mustBe Some("application/json")
    contentAsString(index) must include("errors")
  }

  "renders countries list for authorized user response" in {
    val request = FakeRequest(GET, "/countries").withHeaders("Authorization" -> "token")
    val index = route(app, request).get

    status(index) mustBe OK
    contentType(index) mustBe Some("application/json")
    contentAsString(index) must include("countries")
  }
} 
开发者ID:dsounded,项目名称:money_exchanger,代码行数:51,代码来源:CountriesIndexSpec.scala

示例10: CountriesCreateSpec

//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec

import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._

import play.api.libs.json._

import test.helpers.{DatabaseCleaner, DatabaseInserter}

import play.api.Play

import utils.TimeUtil.now

class CountriesCreateSpec extends PlaySpec with BeforeAndAfterAll {
  val app = new GuiceApplicationBuilder().build

  override def beforeAll() {
    Play.maybeApplication match {
      case Some(app) => ()
      case _ => Play.start(app)
    }

    DatabaseInserter.insert("Users", 12, List("[email protected]", "John", "Doe", "password", "token", now.toString, "Moderator", "1", "999999", "2016-01-01"))
    DatabaseCleaner.clean(List("Countries"))
  }

  override def afterAll() {
    DatabaseCleaner.clean(List("Countries"))
  }

  "saves the record on create" in {
    val request = FakeRequest(POST, "/countries").withJsonBody(Json.parse("""{ "country": {"title":"Germany", "abbreviation":"GER"} }""")).withHeaders("Authorization" -> "token")
    val create = route(app, request).get

    status(create) mustBe CREATED
    contentType(create) mustBe Some("application/json")
    contentAsString(create) must include("country")
  }
} 
开发者ID:dsounded,项目名称:money_exchanger,代码行数:42,代码来源:CountriesCreateSpec.scala

示例11: UsersIndexSpec

//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec

import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._

import play.api.libs.json._

import test.helpers.{DatabaseCleaner, DatabaseInserter}

import play.api.Play

import utils.TimeUtil.now

class UsersIndexSpec extends PlaySpec with BeforeAndAfterAll {
  val app = new GuiceApplicationBuilder().build

  override def beforeAll() {
    Play.maybeApplication match {
      case Some(app) => ()
      case _ => Play.start(app)
    }

    DatabaseCleaner.clean(List("Users"))
    DatabaseInserter.insert("Users", 12, List("[email protected]", "John", "Doe", "password", "token", now.toString, "User", "1", "999999", "2016-01-01"))
  }

  override def afterAll() {
    DatabaseCleaner.clean(List("Users"))
  }

  "renders the index response" in {
    val index = route(app, FakeRequest(GET, "/users").withHeaders("Authorization" -> "token")).get

    status(index) mustBe OK
    contentType(index) mustBe Some("application/json")
    contentAsString(index) must include("users")
    contentAsString(index) mustNot include("authToken")
  }
} 
开发者ID:dsounded,项目名称:money_exchanger,代码行数:42,代码来源:UsersIndexSpec.scala

示例12: UsersShowSpec

//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec

import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._

import play.api.libs.json._

import test.helpers.{DatabaseCleaner, DatabaseInserter}

import play.api.Play

import utils.TimeUtil.now

class UsersShowSpec extends PlaySpec with BeforeAndAfterAll {
  val app = new GuiceApplicationBuilder().build

  override def beforeAll() {
    Play.maybeApplication match {
      case Some(app) => ()
      case _ => Play.start(app)
    }

    DatabaseCleaner.clean(List("Users"))
    DatabaseInserter.insert("Users", 10, List("[email protected]", "John", "Doe", "password", "token", now.toString, "User", "1", "999999", "2016-01-01"))
  }

  override def afterAll() {
    DatabaseCleaner.clean(List("Users"))
  }

  "show response" should {
    "responses 404 if there is no such user" in {
      val show = route(app, FakeRequest(GET, "/users/1").withHeaders("Authorization" -> "token")).get

      status(show) mustBe NOT_FOUND
      contentType(show) mustBe Some("application/json")
      contentAsString(show) mustNot include("user")
    }

    "responses 200 if record exists" in {
      val show = route(app, FakeRequest(GET, "/users/10").withHeaders("Authorization" -> "token")).get

      status(show) mustBe OK
      contentAsString(show) must include("user")
      contentAsString(show) mustNot include("authToken")
    }
  }
} 
开发者ID:dsounded,项目名称:money_exchanger,代码行数:51,代码来源:UsersShowSpec.scala

示例13: UsersCreateSpec

//设置package包名称以及导入依赖的类
import org.scalatest.BeforeAndAfterAll
import org.scalatestplus.play.PlaySpec

import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test.FakeRequest
import play.api.test.Helpers._

import play.api.libs.json._

import test.helpers.DatabaseCleaner

import play.api.Play

class UsersCreateSpec extends PlaySpec with BeforeAndAfterAll {
  val app = new GuiceApplicationBuilder().build

  override def beforeAll() {
    Play.maybeApplication match {
      case Some(app) => ()
      case _ => Play.start(app)
    }

    DatabaseCleaner.clean(List("Users"))
  }

  override def afterAll() {
    DatabaseCleaner.clean(List("Users"))
  }

  "creating" should {
    "returns 400 if bad request" in {
      val request = FakeRequest(POST, "/users").withJsonBody(Json.parse("""{ "user": {"password":"pass"} }"""))
      val create = route(app, request).get

      status(create) mustBe BAD_REQUEST
      contentType(create) mustBe Some("application/json")
      contentAsString(create) must include("errors")
      contentAsString(create) must include("\"email\":[\"must not be empty\"]")
    }

    "returns 201 if request is acceptable" in {
      DatabaseCleaner.clean(List("Users"))

      val request = FakeRequest(POST, "/users").withHeaders("Authorization" -> "token").withJsonBody(Json.parse("""{ "user": {"email":"[email protected]", "password":"pass"} }"""))
      val create = route(app, request).get

      status(create) mustBe CREATED
      contentType(create) mustBe Some("application/json")
      contentAsString(create) must include("user")
      contentAsString(create) must include("authToken")
    }
  }
} 
开发者ID:dsounded,项目名称:money_exchanger,代码行数:54,代码来源:UsersCreateSpec.scala

示例14: DatabaseSpec

//设置package包名称以及导入依赖的类
import java.sql.{Driver, DriverManager}

import org.scalatest.BeforeAndAfter
import org.scalatestplus.play.PlaySpec
import play.api.Logger
import scalikejdbc._
import scalikejdbc.config.DBs

import scala.collection.mutable.Stack

class DatabaseSpec extends PlaySpec with BeforeAndAfter {
  implicit var session = AutoSession

  before {
    DBs.setupAll()
  }

  after {
    DBs.closeAll()
  }

  "Database" must {
    "List of database drivers" in {
      val drivers = DriverManager.getDrivers

      while (drivers.hasMoreElements) {
        val driver:Driver = drivers.nextElement()
        Logger.info(driver.toString)
      }
    }
    "Fetch users" in {
      val sql: SQL[Nothing, NoExtractor] = sql"select * from users"
      val map: SQL[Map[String, Any], HasExtractor] = sql.toMap
      val list: SQLToList[Map[String, Any], HasExtractor] = map.list
      val users: List[Map[String, Any]] = list.apply()
    }
  }

  "Slick" must {
    "How to use Scala test" in {
      // TODO
    }

    "throw NoSuchElementException if an empty stack is popped" in {
      val emptyStack = new Stack[Int]
      a [NoSuchElementException] must be thrownBy {
        emptyStack.pop()
      }
    }
  }
} 
开发者ID:esperia,项目名称:play-scala-sample,代码行数:52,代码来源:DatabaseSpec.scala

示例15: PlaySpecApplication

//设置package包名称以及导入依赖的类
package common

import org.scalatestplus.play.PlaySpec
import play.api.http.{ HeaderNames, HttpProtocol, HttpVerbs, Status }
import play.api.test._

class PlaySpecApplication extends PlaySpec
    with PlayRunners
    with HeaderNames
    with Status
    with HttpProtocol
    with DefaultAwaitTimeout
    with ResultExtractors
    with Writeables
    with RouteInvokers
    with FutureAwaits
    with HttpVerbs 
开发者ID:cm-wada-yusuke,项目名称:ecn-news,代码行数:18,代码来源:PlaySpecApplication.scala


注:本文中的org.scalatestplus.play.PlaySpec类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。