【问题标题】:ScalaMock diffculty mocking HttpResponseScalaMock 难以模拟 HttpResponse
【发布时间】:2020-08-03 12:34:47
【问题描述】:

我正在尝试运行以下测试:

import AV_Enums.TimeSeriesFunctions
import org.scalamock.scalatest.MockFactory
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.must.Matchers.{be, _}
import scalaj.http.HttpResponse

import scala.io.Source

class ResponseParserTest extends AnyFunSuite with MockFactory {


  test("Parsing from a valid response returns a Right") {
    val mockResponse = Source.fromResource("MockResponse.json").mkString

    val mockResponse = mock[HttpResponse[String]]
    (mockResponse.body _).expects().returning(mockResponse)

    ResponseParser.convertTimeSeries(TimeSeriesFunctions.TIME_SERIES_INTRADAY, mockResponse) must be('right)
  }
}

但是我收到以下构建错误,我不明白是什么原因:

type mismatch;
 found   : T
 required: String
    val mockResponse = mock[HttpResponse[String]]
 _ must follow method; cannot follow mockResponse.body.type
    (mockResponse.body _).expects().returning(mockResponse)

我确定我在 ScalaMock 的用法上遗漏了一些东西,但我找不到它是什么。

【问题讨论】:

    标签: scala mocking scalatest scalamock


    【解决方案1】:

    这是 ScalaMock 中的一个已知错误。 尝试下面的解决方法,看看这是否能让你继续前进 - 它会在模拟子类之前锁定类型参数。

    test("Parsing from a valid response returns a Right") {
      val mockResponse = Source.fromResource("MockResponse.json").mkString
      class StringHttpResponse extends HttpResponse[String]("", 0, Map.empty())
      val mockResponse = mock[StringHttpResponse]
      (mockResponse.body _).expects().returning(mockResponse)
    
      ResponseParser.convertTimeSeries(TimeSeriesFunctions.TIME_SERIES_INTRADAY, mockResponse) must be('right)
    }
    

    【讨论】:

    • 我试过了,但我仍然得到:_必须遵循方法;不能跟随 mockResponse.body.type (mockResponse.body _).expects().returning(mockAlphaVantageResponse)
    • 请检查body 是方法还是值/字段。只有方法可以被模拟,听起来好像不是一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    相关资源
    最近更新 更多