标签: specs2

<%运算符在Scala泛型中的含义是什么?

可能重复:
什么是Scala上下文和视图边界?

在specs2中有一个名为Around的方法,这里记录的方法有以下示例:

object http extends Around {
  def around[T <% Result](t: =>T) = openHttpSession("test") {
    t  // execute t inside a http session
  }
}
Run Code Online (Sandbox Code Playgroud)

可以在此处找到此代码的来源.

我很好奇<%运算符在这种情况下意味着什么?

编辑:这里有一个关于这个主题的可靠答案,什么是Scala上下文和视图边界?

generics scala specs2

5
推荐指数
1
解决办法
2685
查看次数

如何使用Mockito验证Specs2中特定字符串匹配器的调用

我按照这些方针进行测试:

httpClient.post(anyString, anyString) returns (first, second)

//do my thing

there were two(httpClient).post(anyString, anyString)
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我想验证第一个调用通过与第二个调用不同的主体.身体相当大,我不想在严格的例子上进行精确匹配.我试过这个:

there was one(httpClientMock).postMessage(anyString, argThat(contain("FOO"))
there was one(httpClientMock).postMessage(anyString, argThat(contain("FOO"))
Run Code Online (Sandbox Code Playgroud)

这让Mockito抱怨​​道:

InvalidUseOfMatchersException: 
 [error] Invalid use of argument matchers!
 [error] 2 matchers expected, 3 recorded:
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

  there was one(httpClientMock).postMessage(argThat(contain("foo")), argThat(contain("FOO")))
  there was one(httpClientMock).postMessage(argThat(contain("foo")), argThat(contain("FOO")))
Run Code Online (Sandbox Code Playgroud)

这导致:

Wanted 1 time:
 [error] -> ...
 [error] But was 2 times. Undesired invocation: ...
Run Code Online (Sandbox Code Playgroud)

在我看来,这样的事情应该是可能的,但我似乎无法弄明白.见解?

scala mockito specs2

5
推荐指数
1
解决办法
8591
查看次数

验证是否使用Mockito调用了方法而未指定参数

使用Mockito,是否可以使用间谍或模拟来验证函数是否被调用/未调用,而不提供实际参数?例如,如果我有一个类或对象:

class MyClass{
  def f(x : Int) = x
}

object MyObject{
  def f(x : Int) = x
}
Run Code Online (Sandbox Code Playgroud)

我想能够说出类似的话:

val my_class = mock[MyClass]
// Do something that causes method f of MyClass to be called
there was one(my_class).f // Doesn't give arguments

val my_object = spy(MyObject)
// Do something that causes method f of MyObject to be called
there was one(my_object).f // Doesn't give arguments
Run Code Online (Sandbox Code Playgroud)

我只是想验证方法是否被调用,而不是它接收到特定的参数.此外,当我检查没有调用函数时:

there was no(my_object).f
Run Code Online (Sandbox Code Playgroud)

我不想验证它是不是用某些参数调用的,而是根本没有调用它.

有没有办法做到这一点?

scala mockito specs2

5
推荐指数
1
解决办法
6846
查看次数

有没有办法在org.spec2.time中排除隐式时间转换并使用自己的?

我正在尝试使用specs2,我有隐式转换的问题,这些转换与我从scala.concurrent.duration._中导入的转换混合,是否有任何方法可以从范围中排除隐含的转换?

import org.specs2.mutable.Specification
import scala.concurrent.duration._

class StatisticsSampleCacheSpec extends Specification {

    val map: Map[Long, Duration] = Map(
        1L -> 5.minute,
        3L -> 3.day,
        5L -> 5.day,
        7L -> 30.day)
}
Run Code Online (Sandbox Code Playgroud)

scala implicit implicit-conversion specs2

5
推荐指数
1
解决办法
664
查看次数

在内存db play框架中运行测试

我正在尝试为我的应用程序运行一些测试.应该可以在一个全新的内存数据库中运行测试,但我不会让它工作.

我的测试现在看起来像这样:

"Server" should {

"persist data for personal user properly" in {
  running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {

    //Create personal users
    val add1 = route(FakeRequest(POST, "/rest/personaluser").withFormUrlEncodedBody("name" -> "user1" , "email" -> "email@test1.com", "password" -> "test123", "gender" -> "male", "birthdate" -> "Oct 1, 2013", "nationality" -> "Sweden")).get
    val add2 = route(FakeRequest(POST, "/rest/personaluser").withFormUrlEncodedBody("name" -> "user2" , "email" -> "email@test2.com", "password" -> "test123", "gender" -> "male", "birthdate" -> "Oct 1, 2013","nationality" -> "Sweden")).get
    status(add1) must equalTo(OK)
    status(add2) must equalTo(OK)

    //Count users
    personalUserRepository.getAllPersonalUsers().length must beEqualTo(2) …
Run Code Online (Sandbox Code Playgroud)

testing automated-tests scala playframework specs2

5
推荐指数
1
解决办法
1137
查看次数

Play Framework,Specs2 - 直接从单元测试调用控制器方法

我的控制器中有一个方法,我想直接打电话.它接受一个POSTed表单,验证它,然后返回一些东西.我想直接测试它 - 即不通过路径助手.

这是我的表单代码(FormFields只是一个案例类)

val searchForm = Form(
  mapping(
   "foo" -> nonEmptyText,
   "filter" -> optional(text).verifying("Filter text must be 'published' or 'unpublished'",
     x => x.isEmpty || x.get == "published" || x.get == "unpublished")
 )(FormFields.apply)(FormFields.unapply)
Run Code Online (Sandbox Code Playgroud)

)

这是我的控制器电话.

def doThings() = IsAuthenticated {
   username => implicit request => {
    searchForm.bindFromRequest().fold(
      formWithErrors => BadRequest(s"Incorrect data: ${formWithErrors.errors.map(x => s"${x.key} ${x.message}").mkString}."),
      form => {
            OK("Test text here")
      }
    )
  }
Run Code Online (Sandbox Code Playgroud)

}

如果我通过我的路由文件调用此方法,如下所示 - 这可以按预期工作.表格被发布,验证,按预期返回OK("测试...").

即.以下工作(使用Specs2)

        val request = FakeRequest(POST, "notarealurl")
          .withFormUrlEncodedBody(
          "filter" -> "published",
          "foo" -> …
Run Code Online (Sandbox Code Playgroud)

post scala playframework specs2 playframework-2.0

5
推荐指数
1
解决办法
3839
查看次数

specs2:如何使用"failtrace"选项

在我的specs2测试中,我经常使用辅助函数来同时测试一组条件.不幸的是,这使得失败测试的行号输出无效,因为所有失败都在同一行.

谷歌出现了一个"故障转移"选项,它将输出堆栈失败的痕迹.但是,我找不到如何实际使用它的示例.它在build.sbt吗?它是否在SBT命令行中使用?它是否在Specification类的构造函数中以某种方式设置?

scala stack-trace specs2

5
推荐指数
1
解决办法
309
查看次数

与specs2 scalaz-stream 0.5a的未解决的依赖关系

自添加:

"org.specs2" %% "specs2" % "2.4.15" % "test" withSources() withJavadoc(),
Run Code Online (Sandbox Code Playgroud)

我得到了我的构建文件

[warn]  Note: Unresolved dependencies path:
[warn]      org.scalaz.stream:scalaz-stream_2.10:0.5a
[warn]        +- org.specs2:specs2_2.10:2.4.15
...
[error] (*:update) sbt.ResolveException: unresolved dependency: org.scalaz.stream#scalaz-stream_2.10;0.5a: not found
Run Code Online (Sandbox Code Playgroud)

scala sbt scalaz specs2

5
推荐指数
1
解决办法
931
查看次数

如何在play scala框架中测试多部分表单数据的操作

我需要一个动作方法来接收文件上传,我也想测试它.但我的测试是抛出错误

我的行动:

def upload = Action.async(parse.multipartFormData) { request =>
  val multipart = request.body
  val optFile = multipart.files.toList.headOption.map(_.ref.file)

  optFile match {
    case None => Future.successful(Ok("got none"))
    case Some(file) => Future.successful(Ok("got some"))
  }
}
Run Code Online (Sandbox Code Playgroud)

我想测试这个方法,但得到错误:

我的考试

"create notes" in {

  val temp = SingletonTemporaryFileCreator.create("test", "png")
  val tempFile = TemporaryFile(temp)
  val filePart = FilePart[TemporaryFile](key = "image", filename = "debug.png", contentType = Some("image/png"), ref = tempFile)
  val form = MultipartFormData(dataParts = Map(), files = Seq(filePart), badParts = Seq(), missingFileParts = Seq())


  val notesController = …
Run Code Online (Sandbox Code Playgroud)

scala multipartform-data specs2 playframework-2.4

5
推荐指数
0
解决办法
730
查看次数

Play Framework和Slick:测试数据库相关的服务

我试图遵循最惯用的方式来进行一些经过全面测试的DAO服务.

我有几个案例类如下:

case class Person (
  id        :Int,
  firstName :String,
  lastName  :String
)

case class Car (
  id      :Int,
  brand   :String,
  model   :String
)
Run Code Online (Sandbox Code Playgroud)

然后我有一个像这样的简单DAO类:

class ADao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends HasDatabaseConfigProvider[JdbcProfile] {
  import driver.api._

  private val persons = TableQuery[PersonTable]
  private val cars = TableQuery[CarTable]
  private val personCar = TableQuery[PersonCar]

  class PersonTable(tag: Tag) extends Table[Person](tag, "person") {
    def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
    def firstName = column[String]("name")
    def lastName = column[String]("description")
    def * = (id, firstName, lastName) <> (Person.tupled, …
Run Code Online (Sandbox Code Playgroud)

testing scala playframework specs2 slick

5
推荐指数
1
解决办法
634
查看次数