0
votes

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.

1
Could you please show more code (including imports)? And what is []? On a first glance your test looks correct.Christian
[], [1,2,3] and [1,2,3,4] are invalid Scala code.Daniel C. Sobral
Also, what's the type signature of Stats.median?Daniel C. Sobral
It was a purely syntactic issue.Squidly

1 Answers

1
votes

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) }
  }
}