0

I'm trying to mock a function like

def foo(x: A, y: B, z: C = blah)

where blah is a java connection object that I don't want to create on the spot

However when I try to stub it like

    (object.foo _)
  .stubs(a, b)

It errors out and says overloaded method value stubs with alternatives... because it's looking for the third parameter. Is there anyway to get around this.

fuzzycuffs
  • 139
  • 1
  • 15

2 Answers2

4

I agree with Matt, but want to point out there is a wildcard syntax in ScalaMock (*) - http://scalamock.org/user-guide/matching/

trait Foo {
  def foo(x: Int, y: Int, z: Int = 0): Int
}

val a: Int = ???
val b: Int = ???
val m = mock[Foo]

m.foo _ stubs(a, b, *)
Philipp
  • 967
  • 6
  • 16
0

You can use a wildcard when you're stubbing out your method.

The following test passes and I think is what you're looking for:

class DefaultParameterTest extends FlatSpec with Matchers with MockFactory {

  class A {
    def foo(x: Int, y: Int, z: Int = 0): Int = 0
  }

  it should "work with a default parameter" in {
    val bar = mock[A]
    (bar.foo _).stubs(1, 2, _: Int).returning(5)

    bar.foo _ expects(1, 2, 0) returning 5 once()

    bar.foo(1, 2)
  }

}
Matt Fowler
  • 2,563
  • 2
  • 15
  • 18