Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore case class field in ScalaTest matching

Assume I have case class with several field members:

case class User(name: String, ..., createdAt: LocalDateTime)

How could I check equality without taking into account createdAt field?

Is there a better way than:

val expected = User("username", stubDate)
actual shouldBe expected.copy(createdAt = actual.createdAt)
like image 541
Anton Feoktistov Avatar asked Dec 22 '22 19:12

Anton Feoktistov


1 Answers

Consider matchPattern like so

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class CaseClassPatternMatchSpec extends AnyFlatSpec with Matchers {
  case class User(name: String, age: Int)

  "User" should "be Worf where we ignore the age" in {
    val actual = User("Worf", 30)
    actual should matchPattern { case User("Worf", _) => }
  }
}

or define custom equality

import org.scalactic.Equality
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class CaseClassPatternMatchSpec extends AnyFlatSpec with Matchers {
  case class User(name: String, age: Int)
  object User {
    implicit val aEq: Equality[User] = (a: User, b: Any) => b match {
      case User(name, _) => a.name == name
      case _ => false
    }
  }

  "User" should "be Worf where we ignore the age " in {
    val actual = User("Worf", 30)
    val expected = User("Worf", -11)
    actual should === (expected)
  }
}

Note only certain matchers can use custom equality

result should equal (3) // can customize equality
result should === (3)   // can customize equality and enforce type constraints
result shouldEqual 3    // can customize equality, no parentheses required
like image 188
Mario Galic Avatar answered Jan 04 '23 22:01

Mario Galic