scala - 是否有规范匹配器可以取消装箱选项和任一

标签 scala bdd specs

我创建了一个规范测试,以验证一些 JSON 解析。虽然测试效果很好,但感觉相当嘈杂。

我想知道规范中是否有现有的代码可以取消装箱选项和任一?

"twitter json to Scala class mapper" should {
    "parsing a tweet" in {
      TwitterJsonMapper.tweetP(tweetS) match {
        case Right(t: Tweet) => {
          implicit def unOption[T](t: Option[T]): T = t.get
          implicit def unEither[T](t: Either[T,Throwable]): T = t match {case Left(left) => left ;case Right(t) => throw t}
          "test id" in {
            true must_== (t.id.get == 228106060337135617l)
          }
          "test id_str" in {
            true must_== (t.id_str.get == "228106060337135617")
          }
          "test time" in {
            true must_== (t.created_at.getHours == 13 )
          }
        }
        case Left((pe: JsonParseException, reason: String)) => fail(reason + "\n" + pe)
      }
    }
  }

 //The Tweet is produced from JSON using Fasterxml's Jackson-Scala library. 
 //I want to use Option or Either monads over all child attributes - for the usual reasons.
case class Tweet(
  @BeanProperty contributors: Option[String],
  @BeanProperty coordinates: Option[String],

  @BeanProperty @JsonDeserialize (
      using = classOf[TwitterDateDeserializer]
  ) created_at: Either[Date,Throwable],
  @BeanProperty favorited: Boolean = false,
  //elided etc etc
  @BeanProperty id_str: Option[String]
}

最佳答案

Option确实有一些特定的匹配器和 Either :

t.id must beSome(228106060337135617l)
t.id_str must beSome("228106060337135617")
t.created_at.left.map(_.getHours) must beLeft(13)

关于scala - 是否有规范匹配器可以取消装箱选项和任一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11708905/

相关文章:

scala - 如何在 ScalaTest 中显示自定义失败消息?

scala - 如何使用 sbt-native-packager publish 包含版本?

eclipse - 在 Scala 2.11 中使用 util.parsing

javascript - 使用 Jasmine Specs 比较对象

iphone - 一个好的 iPhone 应用程序开发环境需要什么规范?

scala - Spark 2.1.1 中获取窗口的最后一个元素

ruby - 我可以覆盖任务 :environment in test_helper. rb 来测试 rake 任务吗?

ruby-on-rails - 如何使用 Capybara 和 RSpec 检查 session 哈希?

cucumber - 在 BDD 用户故事/验收测试中混合“此时”和“何时”

javascript - 使用 TeaSpoon 编写规范时直接使用 PhantomJS 访问页面 - Jasmine