Pete Montgomery
Pete Montgomery

Reputation: 4100

Asserting on case classes in ScalaTest

I see there's support for the Option type, but what about custom case classes?

I sort of want to do this:

result match {
  case SuccessCase(values) => {
    values.foo should be ("bar")
  }
  case FailureCase => // should fail test, but how to say this in ScalaTest?
}

Upvotes: 6

Views: 3131

Answers (3)

reggoodwin
reggoodwin

Reputation: 1544

Bill Venners also suggested writing a custom matcher if writing these kinds of tests often:

https://groups.google.com/forum/?fromgroups#!msg/scalatest-users/4MemQiqLzao/_DsBTQWaqfwJ

Upvotes: 0

Ptharien's Flame
Ptharien's Flame

Reputation: 3246

Does this work, assuming the case you want is DesiredCase?

result match {
  case DesiredCase(values) => {
    values.foo should be ("bar")
  }
  case _ => {
    fail("Not DesiredCase")
  }
}

Upvotes: 2

Bill Venners
Bill Venners

Reputation: 3669

You can use fail() to fail a test on purpose, as in case FailureCase => fail("err msg"), but I'm not sure I understand what you're after. Perhaps you can show more of the code or elaborate to clarify the question?

Upvotes: 4

Related Questions