mic*_*los 1 functional-programming scala specs2 scala-cats cats-effect
我是猫和函数式编程的新手,我正在努力进行单元测试函数数据类型,如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)
specs2-cats提供IOMatchers启用以下语法的特征
library.create(book).value must returnValue(Right(book))
Run Code Online (Sandbox Code Playgroud)
在哪里
libraryDependencies += "org.specs2" %% "specs2-core" % "4.8.1" % Test,
libraryDependencies += "org.specs2" %% "specs2-cats" % "4.8.1" % Test,
Run Code Online (Sandbox Code Playgroud)
这是一个工作示例
import cats.data.EitherT
import cats.effect.IO
import org.specs2.mutable.Specification
import org.specs2.matcher.IOMatchers
class CatsSpec extends Specification with IOMatchers {
case class Book(title: String, year: Int, author: String)
def create(book: Book): EitherT[IO, String, Book] = EitherT(IO(Right(book).withLeft[String]))
val book = Book("Title 1", 2016, "author 1")
"specs2-cats dependency" should {
"provide matcher for IO effect" in {
create(book).value must returnValue(Right(book))
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
335 次 |
| 最近记录: |