标签: specs2

使用specs2指定测试顺序(scala/play框架)

我目前正在使用Specs2库为Scala Play应用程序编写一组测试.

我在编译过程中遇到了一些堆栈溢出错误,因为测试字符串太长,所以我将它拆分成几个类.

问题是测试是使用多线程进程同时运行的.我需要指定那些测试的顺序.有没有办法做到这一点?问候.

scala specs2

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

Scala Spec2 Mockito:非原始参数的匹配器:any,isA

我正在写一个如下的模拟对象:

import org.specs2.mock._
import com...MonetaryValue
import com...Voucher
import org.mockito.internal.matchers._

/**
 * The fake voucher used as a mock object to test other components
 */
case class VoucherMock() extends Mockito {
  val voucher: Voucher = mock[Voucher]

  //stubbing
  voucher.aMethod(any(classOf[MonetaryValue])) answers {arg => //some value to be return based on arg} 

  def verify() = {
    //verify something here
  }
}
Run Code Online (Sandbox Code Playgroud)

存根步骤会抛出异常:

 ...type mismatch;
[error]  found   : Class[com...MonetaryValue](classOf[com...MonetaryValue])
[error]  required: scala.reflect.ClassTag[?]
[error]   voucher.aMethod(any(classOf[MonetaryValue])) answers {arg => //some value to be return based on arg} …
Run Code Online (Sandbox Code Playgroud)

arguments scala matcher mockito specs2

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

猫的匹配器EitherT和效果

我是猫和函数式编程的新手,我正在努力进行单元测试函数数据类型,如EitherT. 有示例代码:

class Library[F[_]]() {
    def create(book: Book)(implicit M: Monad[F]): EitherT[F, BookAlreadyExistsError, Book] = ...
}
Run Code Online (Sandbox Code Playgroud)

我想使用 Spec2 对其进行测试,但我不知道如何正确进行。尝试过这样的事情,但它不起作用:

  val library = Library[IO]()

  test("create book") {

    val book = Book("Title 1", 2016, "author 1")
    (for (
      resultBook <- library.create(book)
    ) yield resultBook shouldEqual ???
    ).unsafeRunSync()

  }
Run Code Online (Sandbox Code Playgroud)

我想有这样的非常简单的断言:

   resultBook shouldEqual Right(Book("Title 1", 2016, "author 1"))
   // or
   resultBook shouldEqual Left(BookAlreadyExistsError)
Run Code Online (Sandbox Code Playgroud)

functional-programming scala specs2 scala-cats cats-effect

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

有没有办法使用规范来模拟 LocalDateTime 时间戳

据推测,我正在为依赖specsScala. 测试更多地绑定到具有某些timestamp属性的事件流。

以下存根是实际实现的部分

private def eligibleForRecentPost(optionalPost: Option[SearchEntity]): Boolean = {
    optionalSearch.map(search => search.timestamp)
      .exists(searchTime => searchTime >= LocalDateTime.now()
        .minusDays(recencyDurationInDays).atZone(ZoneId.systemDefault).toInstant.toEpochMilli)
}
Run Code Online (Sandbox Code Playgroud)

现在,我要查找的代码可能类似于

// just a mock
when(LocalDateTime.now().minusDays(any)
    .atZone(ZoneId.systemDefault).toInstant.toEpochMilli)
    .thenReturn(1579625874972)
Run Code Online (Sandbox Code Playgroud)

请注意,我知道测试中的 search.timestamp 可以更新,但这需要在每次recencyDurationInDays!!

但是在specs2和/或scala中是否有更好更可靠的方法来做到这一点?

编辑:我必须提到,我不期待更改实现以致LocalDateTime被另一个类覆盖/包装。

scala mockito specs2 java-time localdatetime

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

scala上@VisibleForTesting注释的行为

我正在测试我添加到scala类的方法,该构造函数被标记为私有.它可以通过一个单独的对象访问,它可以处理我不想触及的其他许多东西.

class DummyClass private (config: DummyConfig) extends Runnable {

   def refresh() = {
       // Does tons of things
   }

   def updateProperty() = {
      // My method
   }
}

Object DummyClass {
  def apply(config: DummyConfig) = {
     val clss = new DummyClass(config)
     clss.refresh()
     new Thread(clss).start()
     clss
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的JUnit(Specs/Mockito)中,我只想创建该类的对象并测试我添加的单个方法,而不是调用刷新或启动线程.

即使我在类DummyClass上添加guava的注释@VisibleForTesting,我也无法在JUnit测试中使用构造函数.有没有办法实现这一目标?

junit scala mockito guava specs2

0
推荐指数
1
解决办法
1620
查看次数

测试是否在没有模拟的情况下调用函数

假设我有一个功能def bar(y: Int, f: Int => Int): Int.现在我想测试bar使用specs2并确保f被调用.

如何在没有可变变量的情况下在功能上做到这一点.我可以不用嘲笑吗?

unit-testing scala mocking specs2

0
推荐指数
1
解决办法
62
查看次数