Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues with functions returning this.type #209

Open
eprst opened this issue Dec 29, 2017 · 3 comments
Open

issues with functions returning this.type #209

eprst opened this issue Dec 29, 2017 · 3 comments

Comments

@eprst
Copy link

eprst commented Dec 29, 2017

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 #63

thanks

@barkhorn
Copy link
Collaborator

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.

@eprst
Copy link
Author

eprst commented Dec 30, 2017

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

@cspinetta
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants