Scalatest,无法调用invokePrivate

编程入门 行业动态 更新时间:2024-10-26 14:27:32
本文介绍了Scalatest,无法调用invokePrivate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在Scala中测试私有方法,发现Scalatest的PrivateMethodTester可以满足我的需求.但是似乎存在导入问题.

I wanted to test a private method in Scala, and found that Scalatest's PrivateMethodTester does what I need. But there seems to be an import problem.

import org.scalatest._ //Alternatively, I tried //import org.scalatest.PrivateMethodTester //import org.scalatest.FlatSpec "calculateCutoffCriteria" should "give mu -2sigma, mu - sigma, mu + sigma, mu+2sigma as bounds" in { val testData = List(-1, -1, 0, 1, 1) val expected = (-2, -1, 1, 2) val thePrivateMethod = PrivateMethodTester.PrivateMethod[Study]('calculateCutoffCriteria) val actual = Study invokePrivate thePrivateMethod(testData) assert(actual === expected) }

由于某些原因,我不能只调用PrivateMethod[Study],我必须指定PrivateMethodTester.PrivateMethod[Study].而且invokePrivate根本不起作用,整个测试不会编译为invokePrivate不是对象Study成员的错误.

For some reason, I cannot just call PrivateMethod[Study], I have to specify PrivateMethodTester.PrivateMethod[Study]. And invokePrivate doesn't work at all, the whole test doesn't compile with the error that invokePrivate is not a member of the object Study.

我的项目引用了scalatest_2.10.-2.1.0.jar,所有其他测试(不使用PrivateMethodTester)的运行均与预期相同.这是什么问题?

My project references scalatest_2.10.-2.1.0.jar, and all the other tests (which don't use PrivateMethodTester) run just like they should. What's the problem here?

推荐答案

在忘记使用org.scalatest.PrivateMethodTester特性扩展测试类之前,我遇到了这个问题. PrivateMethodTester是为您提供invokePrivate方法的工具.试一试,看看是否有帮助.

I've had this problem before when I forgot to extend my test class with the org.scalatest.PrivateMethodTester trait. The PrivateMethodTester is what gives you your invokePrivate method. Give that a shot and see if that helps at all.

import org.scalatest.PrivateMethodTester class TestSomething extends FlatSpec with PrivateMethodTester { "calculateCutoffCriteria" should "give mu -2sigma, mu - sigma, mu + sigma, mu+2sigma as bounds" in { val testData = List(-1, -1, 0, 1, 1) val expected = (-2, -1, 1, 2) val thePrivateMethod = PrivateMethodTester.PrivateMethod[Study]('calculateCutoffCriteria) val actual = Study invokePrivate thePrivateMethod(testData) assert(actual === expected) } }

更多推荐

Scalatest,无法调用invokePrivate

本文发布于:2023-11-25 05:53:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628581.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Scalatest   invokePrivate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!