Search code examples
unit-testingscalaspecs2

Spec2 unit test is not compiling


I've written a median function and want to add some unit tests for it.

So I wrote this in specs2

class TestStats extends Specification {
  "Median function " should {
    "be None for an empty list" in { Stats.median([]) must beNone }
    "be the midpoint of an odd length list" in { Stats.median([1,2,3]) must_== Some(2)}
    "be the average of the two midpoints of an even length list" in { Stats.median([1,2,3,4])     must_== Some(2.5)}
  }
}

However, it doesn't compile with the error No implicit view available from Option[Double] => org.specs2.execute.Result. on the "be None... line.

I don't understand why it's asking for this in here. Am I really supposed to write an implicit myself to do this comparison?

Edit So the issue was purely syntactical - see my answer below. I'm a little annoyed that a syntax error was reported to me as a semantic error, which is why it never occurred to me that my list literals were wrong.


Solution

  • Clearly, I've spent too long doing Python recently. Correcting the list literal syntax fixes the issue:

    class TestStats extends Specification {
      "Median function " should {
        "be None for an empty list" in { median(Nil) must_== None }
        "be the midpoint of an odd length list" in { median(List(1, 2, 3)) must_== Some(2) }
        "be the average of the two midpoints of an even length list" in { median(List(1, 2, 3, 4)) must_== Some(2.5) }
      }
    }