Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scalamock: How to get "expects" for Proxy mocks?

I am using Scalamock with ScalaTest, and am trying to mock a Java interface. I currently have:

private val _iface = mock [MyInterface]

now I want to do

_iface expects `someMethod returning "foo" once

But the compiler does not find expects.

I imported org.scalatest._ and org.scalamock.scalatest._. What else am I missing?

like image 292
rabejens Avatar asked Oct 20 '22 13:10

rabejens


2 Answers

First of all, proxy mocks are not supported very well in ScalaMock 3, and I think they will be completely removed in ScalaMock 4. Do you really need to use proxy mocks instead macro mocks?

This should work:

package example

import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.scalamock.scalatest.proxy.MockFactory

trait MyInterface {
    def someMethod : String
}

class MyTest extends FlatSpec with Matchers with MockFactory {
  "MyInterface" should "work" in {
    val m = mock[MyInterface]
    m.expects('someMethod)().returning("foo")
    m.someMethod shouldBe "foo"
  }
}

If not, please check ScalaMock proxy mocks unit tests for more examples.

like image 160
Pawel Wiejacha Avatar answered Oct 22 '22 21:10

Pawel Wiejacha


I think it should be something more like:

import org.scalamock.scalatest.MockFactory

class MyTest extends FlatSpec with Matchers with MockFactory {
  "MyInterface" should "work" in {
    val m = mock[MyInterface]
    (m.someMethod _).expects().returning("foo")
    m.someMethod shouldBe "foo"
  }
}

I think the expects arg is expecting the arg to the function

like image 34
vinh Avatar answered Oct 22 '22 21:10

vinh