paulbutcher / ScalaMock

Native Scala mocking framework
http://scalamock.org/
MIT License
501 stars 98 forks source link

Mocking Slick Table results in compile error #507

Open lolgab opened 4 months ago

lolgab commented 4 months ago

ScalaMock Version (e.g. 3.5.0)

6.0.0-M1

Scala Version (e.g. 2.12)

2.13.12

Runtime (JVM or JS)

JVM

Please describe the expected behavior of the issue

It should stub successfully without errors

Please provide a description of what actually happens

Mocking Slick classes fails to compile

Reproducible Test Case

scala-cli test example.test.scala

Gives the following output:

[error] ./example.test.scala:17:21
[error] type mismatch;
[error]  found   : slick.lifted.Query[O(in method ++),Foo#TableElementType,D(in method ++)]
[error]     (which expands to)  slick.lifted.Query[O(in method ++),String,D(in method ++)]
[error]  required: slick.lifted.Query[(some other)O(in method ++),Foo#TableElementType,(some other)D(in method ++)]
[error]     (which expands to)  slick.lifted.Query[(some other)O(in method ++),String,(some other)D(in method ++)]
[error]   val example = stub[FooQuery]
[error]     
// example.test.scala

//> using scala 2
//> using lib org.scalamock::scalamock:6.0.0-M1
//> using lib org.scalatest::scalatest:3.2.18
//> using lib com.typesafe.slick::slick:3.4.1

import org.scalamock.scalatest.MockFactory
import org.scalatest.funsuite.AnyFunSuite
import slick.jdbc.H2Profile.api._

class Foo(tag: Tag) extends Table[String](tag, "FOO") {
  def * = column[String]("foo")
}
class FooQuery extends TableQuery(new Foo(_))

class test extends AnyFunSuite with MockFactory {
  val example = stub[FooQuery]
}

Self-contained reproduction

import org.scalamock.scalatest.MockFactory
import org.scalatest.funsuite.AnyFunSuite

class Foo extends Query
class Query[+E, U, C[_]] {
  def bar[O >: E, R, D[_]](other: Query[O, U, D]) = ???
}

class test extends AnyFunSuite with MockFactory {
  val example = stub[Foo]
}
goshacodes commented 4 months ago

Hi! If you could provide a corresponding example without slick that would spare a huge amount of time for me

lolgab commented 4 months ago

Hi @goshacodes, I managed to extract a self-contained reproduction and added it to the issue description.

goshacodes commented 4 months ago

Thank you!

goshacodes commented 3 months ago

Hi again, I think it is impossible to make this work with scala 2. Works with scala 3 and latest slick

lolgab commented 3 months ago

@goshacodes Good to know! One more reason to migrate to Scala 3. You can work around the issue by defining a trait and mocking that instead of mocking the class itself, which is what we are doing.