简体   繁体   中英

How to invokePrivate on a function with implicit parameters

How do I invoke XYZ.doSomething method for these classes:

XYZ.scala

object XYZ {

  private def doSomething(i : Int)(implicit s: String): String {
    s + i.toString
  }
}

XYZTest.scala

class XYZTest extends FunSpec with PrivateMethodTester {
  describe("SomeTest") {
    it("Can't find a private method named: doSomething :( ") {
      implicit lazy val someStr: String = "sahil"
      val doSomething = PrivateMethod[String]('doSomething)
      val myStr = XYZ invokePrivate doSomething(1)
      assert(myStr == "sahil1")
    }
  }
  describe("SomeTest") {
    it("This doesn't even compile :( ") {
      val doSomething = PrivateMethod[String]('doSomething)
      val myStr = XYZ invokePrivate doSomething(1)("sahil")
      assert(myStr == "sahil1")
    }
  }
}

Correct answer is:

object XYZ {

  private def doSomething(i : Int)(implicit s: String): String = {
    s + i.toString
  }
}

class XYZTest extends FunSpec with PrivateMethodTester {
  describe("SomeTest") {
    it("Can't find a private method named: doSomething :( ") {
      implicit lazy val someStr: String = "sahil"
      val doSomething = PrivateMethod[String]('doSomething)
      val myStr = XYZ invokePrivate doSomething(1, someStr)
      assert(myStr == "sahil1")
    }
  }
}

Brief view on invokePrivate method shows that it does not support implicits , but looks like it treats currying argument list as general sequence of arguments

def invokePrivate[T](invocation : PrivateMethodTester.this.Invocation[T])

where args is the sequence:

final class Invocation[T](val methodName : scala.Symbol, val args : scala.Any*)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM