【问题标题】:A simple Scala Given/When/Then style specification failed一个简单的 Scala Given/When/Then 样式规范失败
【发布时间】:2013-04-18 17:20:32
【问题描述】:

我是 Spec2 的新手,正在努力学习它。我想出了以下代码,

@RunWith(classOf[JUnitRunner])
class GWTStyleSpec extends Specification {
    "A given-when-then example for the addition" ^
       "Given the following number: ${1}" ^ number1 ^
       "And a second number: ${2}" ^ number2 ^
       "Then I should get: ${3}" ^ result ^
     end
   val number1: Given[Int] = (_: String).toInt
   val number2: When[Int, (Int, Int)] = (n1: Int) => (s: String) => (n1, s.toInt)
   val result: Then[(Int, Int)] = (n: (Int, Int)) => (s: String) => ((n._1 + n._2) must_== s.toInt)
}

运行它后,我得到 java.lang.Exception: Could not instantiate class com.me.scala.start.GWTStyleSpec: null 并且下面有很多异常堆栈跟踪。

我做错了什么?

【问题讨论】:

    标签: scala specs2


    【解决方案1】:

    如果您要导入 org.specs2.Specification,那么应该定义了 def is = ... 方法:

    @RunWith(classOf[JUnitRunner])
    class GWTStyleSpec extends Specification { def is = 
      "A given-when-then example for the addition" ^
         "Given the following number: ${1}"        ^ number1 ^
         "And a second number: ${2}"               ^ number2 ^
         "Then I should get: ${3}"                 ^ result ^
                                                   end
      lazy val number1: Given[Int] = (_: String).toInt
      lazy val number2: When[Int, (Int, Int)] = (n1: Int) => (s: String) => (n1, s.toInt)
      lazy val result: Then[(Int, Int)] = (n: (Int, Int)) => (s: String) => ((n._1 + n._2) must_== s.toInt)
    }
    

    vals 也可能没有正确实例化,因此您可以尝试使用lazy vals。

    另请注意,下一个 specs2 版本 (1.15-SNAPSHOT) 提出了基于 Scala 2.10 功能的 Given/When/Then 规范的another style

    class GivenWhenThenInterpolatedSpec extends Specification with GivenWhenThen { def is = sequential ^ s2"""                   
    
     A given-when-then example for a calculator                                                                                  
       Given the following number: 1                             $aNumber                                                        
       And a second number: 2                                    $aNumber                                                        
       And a third number: 6                                     $aNumber                                                        
       When I use this operator: +                               $operator                                                       
       Then I should get: 9                                      $result                                                         
       And it should be >: 0                                     $greaterThan                                                    
    
     Now with the multiplication                                                                                                 
       Given the following number: 4                             $aNumber                                                        
       And a second number: 5                                    $aNumber                                                        
       And a third number: 6                                     $aNumber                                                        
       When I use this operator: *                               $operator                                                       
       Then I should get: 120                                    $result                                                         
       And it should be >: 10                                    $greaterThan                                                    
       But not should be >: 150                                  $lowerThan                                                      
                                                                 """                                                             
    
      val readInt = groupAs("\\d+")                                                                                              
      val readOperator = readAs(".*: (.)$")                                                                                      
      val aNumber: Given[Int] = readInt and { s: String => s.toInt }                                                             
    
      // when there are too many Given[T, S] consecutive steps, it is possible to follow them with a When[Seq[T], S]             
      val operator: When[Seq[Int], Operation] = readOperator and { (numbers: Seq[Int]) => (s: String) => Operation(numbers, s) } 
    
      val result: Then[Operation] = readInt andThen { (operation: Operation) => (s: String) =>                                   
        operation.calculate  must_== s.toInt                                                                                     
      }                                                                                                                          
      val greaterThan: Then[Operation] = readInt andThen { (operation: Operation) => (s: String) =>                              
        operation.calculate  must be_>= (s.toInt)                                                                                
      }                                                                                                                          
      val lowerThan: Then[Operation] = readInt andThen { (operation: Operation) => (s: String) =>                                
        operation.calculate  must be_<= (s.toInt)                                                                                
      }                                                                                                                          
    
      case class Operation(numbers: Seq[Int], operator: String) {                                                                
        def calculate: Int = if (operator == "+") numbers.sum else numbers.product                                               
      }                                                                                                                          
    
    }                                                                                                                            
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多