在 Scala 中测试高阶函数

Testing higher order functions in scala

我有一个案例,我有一个可以取消特定项目(命令)资格的规则列表。每个规则都是一个高阶函数。 (为了可重用性)。

最后我需要验证是什么规则导致了失败。我如何确定导致失败的规则?

有没有办法获取函数来源的函数名称?

当我测试并 return 返回导致失败的函数时,我得到一个非描述性的“”,它与实际 returned 的内容无关。

这方面的一个例子:

   object Library { 
       def testIf5(v:Int): Boolean = { v==5} 
   }

   class CommandVerifier { 
      def doesItFail(value: Int, rules: List[(Int)=>Boolean]) {
           rules.find(r=> !r(value))
      }

   }

   val expected = testIf5
   val actaul = new CommandVerifier().doesItFail(5, List(testIf5))
   actual should be expected (expected) 

如果你想在运行时得到函数名,那是不可能的,因为函数在运行时没有名字。参见 and How do I get the function name in Scala?。我认为您应该独立测试这些规则