Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
}
like image 672
Pete Montgomery Avatar asked Jul 27 '12 12:07

Pete Montgomery


People also ask

Does ScalaTest use JUnit?

Also, you can use JUnit if you want to, and import the ScalaTest or the specs2 matchers inside your test cases if you want more expressivity for your assertions.

How do I ignore ScalaTest?

When you mark a test class with a tag annotation, ScalaTest will mark each test defined in that class with that tag. Thus, marking the SetSpec in the above example with the @Ignore tag annotation means that both tests in the class will be ignored.

What is assertion in Scala?

assert is a precondition in Scala that evaluates a boolean expression as true or false. It's generally used to validate the execution of the program during runtime. We can see assert is used commonly in testing the functionality of programs.


2 Answers

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?

like image 137
Bill Venners Avatar answered Sep 28 '22 04:09

Bill Venners


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

result match {
  case DesiredCase(values) => {
    values.foo should be ("bar")
  }
  case _ => {
    fail("Not DesiredCase")
  }
}
like image 25
Ptharien's Flame Avatar answered Sep 28 '22 05:09

Ptharien's Flame