Search code examples
scalainheritancematcherspecs2

How to compare result type to be subclass of abstract class to match to?


I have a method with signature

def save(u: User): Future[Option[UserServiceError]]

that I want to test with Specs2.

I've got the following type hierarchy:

abstract class UserServiceError(message: String)
object UserAlreadyExist extends UserServiceError("User already exists")

then, test code looks like this:

users.save(...) must beSome(users.UserAlreadyExist).await

so I expect matcher beSome to understand that UserAlreadyExist is a subtype of UserServiceError from method signature.

However, it gives me an compile time type check error

type mismatch;
[error]  found   : org.specs2.matcher.Matcher[scala.concurrent.Future[Option[core.model.services.UserService#UserAlreadyExist.type]]]
[error]  required: org.specs2.matcher.Matcher[scala.concurrent.Future[Option[core.model.services.UserService#UserServiceError]]]

I have an inkling that I miss something very basic here, like UserAlreadyExist.type isn't supposed to conform with UserServiceError[without .type].

Where am I doing it wrong?


Solution

  • I didn't taken into account that you can have matchers inside beSome() so with next changes test code works (see beEqualTo):

    must beSome(beEqualTo(UserService.UserAlreadyExist)).await
    

    I moved UserServiceError's into UserService's companion object also.