首页 > 解决方案 > scala mockito错误`value thenReturn不是Nothing的成员`

问题描述

  val users = List(User(name = "A"))
  val userRepoMock = mock[UserRepo]

  "GET /users" should {
    "return the users" in {
      when(userRepoMock.get()).thenReturn(Future.successful(Good(users)))

当我运行测试时,它不会编译

   value thenReturn is not a member of Nothing
   [error]      L55:      
   when(userRepoMock.get()).thenReturn(Future.successful(Good(users)))
   [error]      L55:          

谁能帮我解决这个问题,好吗?提前致谢

这是 UserRepo 的定义

trait UserRepo {
  def get(): Future[List[User]]
}

object UserRepo {

  class ActorImpl @Inject()(actor: UserSyncActor.Ref) extends UserRepo {
    override def get(): Future[List[User]] = {
      implicit val timeout: Timeout = 10.seconds
      actor.ref.ask(UserSyncActor.GetUsers).mapTo[List[User]]
    }
  }
}

我还在一个模块中将 UserRepo 与其 ActorImpl 链接起来

class ActorsModule extends AbstractModule {
  ...

  override def configure(): Unit = {
    val _ = bind(classOf[UserRepo]).to(classOf[UserRepo.ActorImpl])
  }
}

标签: scalaunit-testingmockitoactor

解决方案


我最近遇到了类似的问题,对我来说,问题是我在里面返回的thenReturn()类型与我正在模拟的函数应该返回的类型不同。

在您的情况下,我会仔细检查,Future.successful(Good(users))Future[List[User]]此错误消息非常模糊且无益,因此可能是其他内容


推荐阅读