ScalaMock icon indicating copy to clipboard operation
ScalaMock copied to clipboard

issues with functions returning `this.type`

Open eprst opened this issue 7 years ago • 3 comments

ScalaMock Version (e.g. 3.5.0)

4.0.0

Scala Version (e.g. 2.12)

2.11

Runtime (JVM or JS)

JVM

Please describe the expected behavior of the issue

There should be a way to mock traits/classes that have functions returning this.type

Please provide a description of what actually happens

There's either a compile-time error or a NoSuchMethod (with proxies)

Reproducible Test Case

  it should "work" in {
    trait A { def foo: this.type }

    val a = stub[A]
    a.foo
    (a.foo _).verify
  }

You can try this with both MockFactory and MockFactory with ProxyMockFactory. First one results in compile error:

overriding method foo in trait A of type => xx.XxSpec.$anon.type;
method foo has incompatible type

Second one - in NoSuchMethod:

java.lang.NoSuchMethodException: com.sun.proxy.$Proxy7.mock$foo$0()

This is probably related to https://github.com/paulbutcher/ScalaMock/issues/63

thanks

eprst avatar Dec 29 '17 03:12 eprst

You should be extending your test suite with org.scalamock.scalatest.proxy.MockFactory, org.scalamock.scalatest.MockFactory or org.scalamock.scalatest.MixedMockFactory. Worth keeping in mind that the Proxy mocks are limited by what the API for proxies can supply (fully abstract interfaces without implementations). Not sure how well that works in practice with the Scala type system though, I use them rarely.

For the first invocation, at first glance this looks like another issue with the way types are handled in our macros. Similar to #60 . Not sure if this is fixable though without switching to scala-meta (basically a rewrite of ScalaMock then). I'll take a look.

barkhorn avatar Dec 29 '17 08:12 barkhorn

I probably got to MockFactory with ProxyMockFactory by following some example from Stackverflow. Slightly updated version:

org.scalamock.scalatest.MockFactory -> compile-time error org.scalamock.scalatest.proxy.MockFactory -> run-time error

proxies should work in this case as we're mocking a simple trait, similar to Java interface

thanks

eprst avatar Dec 30 '17 23:12 eprst

Hi guys! Any advance in this issue? I'm trying to mock a class which mixes a Trait with this.type as return type on some methods and I'm being blocked by this issue.

cspinetta avatar Jul 24 '19 21:07 cspinetta