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

Mock (lazy) vals #40

Closed
rkaercher opened this issue Apr 20, 2013 · 5 comments
Closed

Mock (lazy) vals #40

rkaercher opened this issue Apr 20, 2013 · 5 comments
Projects

Comments

@rkaercher
Copy link

I want to mock classes having vals and lazy vals. When I try this:

import org.scalamock.scalatest.MockFactory
import org.scalatest.FunSpec

class LazyValTest extends FunSpec with MockFactory {

  describe("A mocked instance should have its lazy vals mockable.") {

    class ClassToMock {
      lazy val mockMe = 5 * 3
      val mockMeToo = 5

      def getLazyVal = mockMe
      def getNormalVal = mockMeToo
    }

    val mockedInstance = stub[ClassToMock]

    (mockedInstance.getNormalVal _) expects () returning 3

    (mockedInstance.getLazyVal _) expects () returning 5

    (mockedInstance._mockMe _) expects () returning 5

    (mockedInstance.mockMeToo _) expects () returning 3
  }

}

I get the following compile error:
_ must follow method; cannot follow mockedInstance.mockMe.type

Am I doing this wrong or is this an error?

Thanks in advance for your help.

@paulbutcher
Copy link
Owner

This is expected behaviour because Scala doesn't allow a lazy val to be overridden with a def. It's possible that I might be able to make it work in ScalaMock4, however, which I've just started working on.

@barkhorn barkhorn added this to the v4.0.0 milestone Dec 5, 2016
@barkhorn barkhorn changed the title Mocking (lazy) vals fails in ScalaMock 3.0.1 Mock (lazy) vals Jan 22, 2017
@barkhorn
Copy link
Collaborator

barkhorn commented Mar 3, 2017

This is now mentioned as a known limitation in the FAQ: http://scalamock.org/user-guide/faq/

@steinybot
Copy link

There is a comment from @paulbutcher on StackOverflow about scalameta making this possible. Is this now something that could be implemented?

@barkhorn
Copy link
Collaborator

ScalaMeta is an interesting library but it will require significant rewrites to make use of it. ScalaMock relies on whitebox macros internally, which aren't exactly compatible and we'd need to transition away from that. Still worth looking at.

@barkhorn
Copy link
Collaborator

a Scala val cannot be overridden by a def, so this feature doesn't fit into ScalaMock really, even with the potential use of ScalaMeta. Assuming the val is not final, you can just subclass it yourself so i don't really see a strong argument for ScalaMock to provide special handling. Example:

class ClazzIWantToMock {
  val thingIdlikeToOverride: Int = 5
}

class MockedClazz extends ClazzIWantToMock {
  override val thingIdlikeToOverride= 99
}

ScalaMock automation moved this from Blocked to Done Feb 22, 2020
@barkhorn barkhorn removed this from the next-gen milestone Feb 22, 2020
@barkhorn barkhorn removed the feature label Feb 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

5 participants