【问题标题】:scala mockito error `value thenReturn is not a member of Nothing`scala mockito错误`value thenReturn不是Nothing的成员`
【发布时间】:2020-09-01 14:53:51
【问题描述】:
  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])
  }
}

【问题讨论】:

  • 我不确定这是如何编译的。 Future[Good[List[User]]] 与 Future[List[User]]] 的类型不同。尝试编写此模拟时,您应该会遇到编译错误。也许如果你把整个测试代码放上去,就更容易看到到底发生了什么。

标签: scala unit-testing mockito actor


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多