【问题标题】:Specs2/Neo4j - Using ImpermanentGraphDatabase with Specs2Specs2/Neo4j - 将 ImpermanentGraphDatabase 与 Specs2 一起使用
【发布时间】:2013-02-13 03:54:09
【问题描述】:

我正在使用 Specs2 编写验收测试。

我想使用ImpermanentGraphDatabase 以获得内存中的 Neo4j 图表;非常适合集成测试。

我为 Neo4j 设置了 Spring-Data,我的 Spring 文件配置包含:

<bean id="graphDatabaseService" class="org.neo4j.test.ImpermanentGraphDatabase" destroy-method="shutdown"/>

我想知道 destroy-method="shutdown" 选项是否考虑到 Specs2 而不是通常的 JUnit,以便隔离每个 Spec 的 example

总结一下:每个example 是否都有自己的内存中图实例,或者将共享给所有的人?

我猜它不适用,因为 specs2 对所有这些 Spec 的示例执行使用相同的 Specification 实例。事实上,在 Specs2 的函数式风格中,只有 is() 方法被称为将所有示例包含在一个实例中。

我还尝试实现 BeforeExample 特征,以便在每个 example 处清理数据库,但是...使用 Given/Then/When 样式,似乎整个被认为是唯一的 example。事实上,分隔符是^,而不是传统的!,后者代表一个example

我想在每一步之前清理内存数据库(ImpermanentGraphDatabse)(GivenWhenThen 步骤)

【问题讨论】:

    标签: neo4j spring-data-neo4j specs2


    【解决方案1】:

    我对您的问题的理解是,您希望在每组 Given/When/Then 步骤之前有一个“新鲜”的数据库。

    为此,您可以:

    • 在每组 Given/When/Then 步骤之前明确指定操作

      Step(cleanupDatabase) ^
      "A given-when-then example for the addition" ^
        "Given the following number: ${1}"         ^ number1 ^
        "And a second number: ${2}"                ^ number2 ^
        "And a third number: ${3}"                 ^ number3 ^
                                                   end^
      Step(cleanupDatabase) ^
      "A given-when-then example for the addition" ^
       "Given the following number: ${1}"         ^ number1 ^
       "And a second number: ${2}"                ^ number2 ^
       "And a third number: ${3}"                 ^ number3 ^
                                               end
      
    • 使用函数来声明每个组并在每个组之前映射一个清理步骤

      def `first example` =     
        "A given-when-then example for the addition" ^
          "Given the following number: ${1}"         ^ number1 ^
          "And a second number: ${2}"                ^ number2 ^
          "And a third number: ${3}"                 ^ number3 ^
                                                     end
      def `second example` =     
        "A given-when-then example for the addition" ^
          "Given the following number: ${1}"         ^ number1 ^
          "And a second number: ${2}"                ^ number2 ^
          "And a third number: ${3}"                 ^ number3 ^
                                                     end
      
      def is = Seq(`first example`, `second example`).foldLeft(Step():Fragments) { (res, cur) =>
        res ^ Step(cleanupDatabase) ^ cur 
      }
      
    • 使用map function of the Specification 来做一般性操作

      override def map(fs: =>Fragments) = fs.flatMap {
        // clean the database at the end of a G/W/T block
        case f if f == end => Seq(Step(cleanDatabase), end)
        case other         => Seq(other)    
      }
      

    【讨论】:

    • 我喜欢最后一个建议,但 End() 是一个不可访问的 case class(因为它的 englobing 对象带有 private[specs2] 注释)。
    • 对,我删除了这个限制,但也检查了我之前没有编译的答案!我将编辑该答案并使用最新的 1.12.4-SNAPSHOT 使其变得更好。
    猜你喜欢
    • 2013-02-04
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多