0

I am trying to run the following test:

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

However I am getting the following build error which I can't understand what is the cause:

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

I am sure I am missing something on ScalaMock's usage but I can't find what it is.

LeYAUable
  • 1,613
  • 2
  • 15
  • 30

1 Answers1

0

This is a known bug in ScalaMock. Try the workaround below and see if this gets you going - it locks the type paramter down before mocking the subclass.

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)
}
Philipp
  • 967
  • 6
  • 16
  • I tried it, but I am still getting: _ must follow method; cannot follow mockResponse.body.type (mockResponse.body _).expects().returning(mockAlphaVantageResponse) – LeYAUable Apr 21 '20 at 17:05
  • please check if `body` is a method or value/field. Only methods can be mocked and it sounds like it's not one. – Philipp Apr 22 '20 at 16:34