我有功能A => Double.我想检查两个这样的函数是否beCloseTo为给定的值集给出相同的结果(使用现有匹配器的公差).
我希望能够写:
type TF = A => Double
(f: TF) must computeSameResultsAs(g: TF,tolerance: Double, tests: Set[A])
Run Code Online (Sandbox Code Playgroud)
我想以模块化的方式构建这个匹配器,而不是简单地Matcher[TF]从头开始编写.
如果我能写的话可能会更好:
(f: TF) must computeSameResultsAs(g: TF)
.withTolerance(tolerance)
.onValues(tests: Set[A])
Run Code Online (Sandbox Code Playgroud)
我还想在匹配器失败时获得合理的描述.
睡了之后我想出了以下内容.
def computeSameResultsAs[A](ref: A => Double, tolerance: Double, args: Set[A]): Matcher[A => Double] =
args.map(beCloseOnArg(ref, tolerance, _)).reduce(_ and _)
def beCloseOnArg[A](ref: A => Double, tolerance: Double, arg: A): Matcher[A => Double] =
closeTo(ref(arg), tolerance) ^^ ((_: A => Double).apply(arg))
Run Code Online (Sandbox Code Playgroud)
这比Eric的解决方案短得多,但没有提供良好的失败信息.我希望能够在第二种方法中重命名映射值.像下面的东西(不编译).
def beCloseOnArg[A](ref: A => …Run Code Online (Sandbox Code Playgroud) 因此,游戏框架谈论有specs2和specs2有mockito
我想使用mockito编写一个测试,其中控制器调用的模板是mockito mock.
到目前为止,我发现的所有文档都是java实现,您可以在其中调用mock静态函数并将其作为泛型参数赋予Mocked类.
据我所知,模拟函数默认情况下不会在规范中公开,所以如何创建mockito mock?
请给出一个示例,其中包括创建模拟和断言使用某些参数调用模拟
按此处所述运行测试
"Spec" should {
"example" in new WithApplication {
...
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说慢得令人无法接受.这是因为新的WithApplication在每个示例中都在启动和停止框架.不要误解我的意思,框架本身加载速度非常快,但如果数据库配置好了(惊喜!),情况会变得很糟糕.
以下是一些测量:
"The database layer" should {
"test1" in {
1 must be equalTo(1)
}
...
"test20" in {
1 must be equalTo(1)
}
}
Run Code Online (Sandbox Code Playgroud)
执行时间:2秒.在每个示例中使用WithApplication的相同测试消耗9秒
由于这个答案,我能够取得更好的结果
import play.api.Play
import play.api.test.FakeApplication
import org.specs2.mutable.Specification
import scalikejdbc._
class MySpec extends Specification {
var fake: FakeApplication = _
step {fake = FakeApplication(...)}
step {Play.start(fake)}
"The database layer" should {
"some db test" in { …Run Code Online (Sandbox Code Playgroud) 我是Scala和Spec2的新手.
我想创建以下测试,但我从编译器得到一个错误.
这是我想写的测试
import org.specs2.mutable._
import org.specs2.specification._
import org.specs2.matcher._
import org.specs2.matcher.MatchResult
class SimpleParserSpec extends Specification {
"SimpleParser" should {
val parser = new SimpleParser()
"work with basic tweet" in {
val tweet = """{"id":1,"text":"foo"}"""
parser.parse(tweet) match {
case Some(parsed) => {
parsed.text must be_==("foo")
parsed.id must be_==(1)
}
case _ => failure("didn't parse tweet")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:C:\ Users\haques\Documents\workspace\SBT\jsonParser\src\test\scala\com\twitter\sample\simpleSimpleParserSpec.scala:17:找不到类型组织的证据参数的隐含值. specs2.execute.AsResult [对象]
问候,
Shohidul
我已经习惯了JUnit,在JUnit中,只需在单个文件(类)中定义这些测试并使用它们进行注释,就可以将多个测试(通常与类相关)组合在一起@Test.然后,要运行其中几个测试,TestSuite使用@Suite.SuiteClasses依此类推创建a .
在specs2中,可以将两个不同级别的测试分组,扩展一些Specification.例如:
"Whatever" should {
"do its job when possible" in {
whatever(new Thing).work must beSome
}
"return none when not possible" in {
whatever(null).work must beNone
}
}
Run Code Online (Sandbox Code Playgroud)
我们可以Specification将这种类型中的几个组合在一个文件中,并且每个都可以打包几个检查,每个检查就像一个@Test,每个组像JUnit中的文件一样,然后每个都Specification作为SuiteJUnit中的一个,除了a Suite被分成几个类和a Specification在单个类(即文件)中,这往往会产生巨大的文件.
所以问题有两个:
Specification和每个班级应该做的事情,即它应该通过的检查.Suite如果可能的话以分层方式对它们进行分组,例如Suites对于ScalaTest.BTW:我使用Specs2因为我觉得这是标准的(默认情况下是与原型,一个(非常小)小(传闻)样本证实了这一[ 1,2 ]),但我正在考虑使用ScalaTest.根据数字(specs2,scalatest)判断,这可能是遵循Scala社区标准和习俗的最佳选择.我之所以提到这一点是因为出于这些原因,"不可能使用ScalaTest"这样的答案是可以接受的.
假设我在"单元"样式中定义了specs2规范,如下所示:
import org.specs2.mutable
class MyClassSpec extends mutable.Specification {
"myMethod" should {
"return positive values" in {
MyClass.myMethod must beGreaterThan(0)
}
"return values less than 100" in {
MyClass.myMethod must beLessThan(100)
}
}
}
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法可以跳过/禁用/标记待定块/片段中的所有示例myMethod?
显然,我可以从块中的每个单独的示例调用pendingUntilFixed或返回pending,但对于具有许多规范的块而言,这将是相当繁琐的.
如果MyClass.myMethod难以实施并受到惩罚,这似乎是常见的事情.还有另一种方法,这通常在specs2中完成吗?
我想将Specs2测试结果与Jenkins集成.
我在sbt中添加了以下属性:
解析:
"maven specs2" at "http://mvnrepository.com/artifact"
Run Code Online (Sandbox Code Playgroud)
libraryDependencies:
"org.specs2" %% "specs2" % "2.0-RC1" % "test",
Run Code Online (Sandbox Code Playgroud)
系统属性:
testOptions in Test += Tests.Setup(() => System.setProperty("specs2.outDir", "/target/specs2-reports")) //Option1
//testOptions in Test += Tests.Setup(() => System.setProperty("specs2.junit.outDir", "/target/specs2-reports")) //Option2
testOptions in Test += Tests.Argument(TestFrameworks.Specs2, "console", "junitxml")
Run Code Online (Sandbox Code Playgroud)
如果我运行以下命令,它不会在上述目录中生成任何规范报告("/ target/specs2-reports").
sbt>测试
如果我运行以下命令,它会询问目录,如下面的错误消息所示:
sbt>仅限测试 - junitxml
[error]无法运行测试代码.model.UserSpec:java.lang.IllegalArgumentException:junitxml需要指定目录,例如:junitxml(directory ="xxx")
它只有在我给出如下所示的目录时才有效:
sbt>仅限测试 - junitxml(directory ="\ target\specs-reports")
但有时它没有生成所有规格报告xmls(有时只生成一个报告,有时只生成两个报告等).
如果我在jenkins中给出仅测试 - junitxml(directory ="\ target\specs-reports"),则会给出以下错误.
[error] Not a valid key: junitxml (similar: ivy-xml)
[error] junitxml(
[error] ^
Run Code Online (Sandbox Code Playgroud)
我的主要目标是,我想以junit xml格式生成整合的测试报告并与Jenkins集成.请帮助我解决我的问题. …
我一直在使用官方Play文档中的Specs2来跟踪Scala测试示例.我注意到他们WithApplication用来启动一个虚假的应用程序进行测试,如下所示:
"something" should {
"do X" in new WithApplication { /* ... */ }
"do Y" in new WithApplication { /* ... */ }
"do Z" in new WithApplication { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)
这很好,但是我遇到的问题是,每次发生这种情况时,我都要承担启动应用程序的成本.一旦您的测试套件增长到合理的大小,这不一定是"快"或至少不够快.我尝试过这样的事情:
val app = FakeApplication()
"something" should {
"do X" in new WithApplication(app) { /* ... */ }
"do Y" in new WithApplication(app) { /* ... */ }
"do Z" in new WithApplication(app) { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)
和
"something" should { …Run Code Online (Sandbox Code Playgroud) testOnly play.api.weibo.StatusesShowBatchSpec
[error] Could not create an instance of play.api.weibo.StatusesShowBatchSpec
[error] caused by java.lang.Exception: Could not instantiate class play.api.weibo.StatusesShowBatchSpec: null
[error] org.specs2.reflect.Classes$class.tryToCreateObjectEither(Classes.scala:93)
[error] org.specs2.reflect.Classes$.tryToCreateObjectEither(Classes.scala:211)
[error] org.specs2.specification.SpecificationStructure$$anonfun$createSpecificationEither$2.apply(BaseSpecification.scala:119)
[error] org.specs2.specification.SpecificationStructure$$anonfun$createSpecificationEither$2.apply(BaseSpecification.scala:119)
...
Run Code Online (Sandbox Code Playgroud)
规范
package play.api.weibo
import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner
class StatusesShowBatchSpec extends ApiSpec {
"'statuses show batch' api" should {
"read statuses" in {
val api = StatusesShowBatch(
accessToken = testAdvancedToken,
ids = "3677163356078857")
val res = awaitApi(api)
res.statuses must have size (1)
}
Run Code Online (Sandbox Code Playgroud)
}}
请在此处查看完整代码https://github.com/jilen/play-weibo/tree/spec2_error
玩2.4应用程序,使用服务类的依赖注入.
我发现当被测试的服务类具有多个注入依赖项时,Specs2会发生阻塞.它失败了" 找不到类的构造函数...... "
$ test-only services.ReportServiceSpec
[error] Can't find a constructor for class services.ReportService
[error] Error: Total 1, Failed 0, Errors 1, Passed 0
[error] Error during tests:
[error] services.ReportServiceSpec
[error] (test:testOnly) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 2 s, completed Dec 8, 2015 5:24:34 PM
Run Code Online (Sandbox Code Playgroud)
生产代码,剥离到最低限度以重现此问题:
package services
import javax.inject.Inject
class ReportService @Inject()(userService: UserService, supportService: SupportService) {
// ...
}
class UserService {
// ...
}
class SupportService {
// ...
}
Run Code Online (Sandbox Code Playgroud)
测试代码:
package …Run Code Online (Sandbox Code Playgroud) dependency-injection scala playframework specs2 playframework-2.4