具有ScalaTest行为功能的ScalaCheck生成器

时间:2017-06-22 07:19:41

标签: scala scalatest scalacheck

我正在尝试将ScalaCheck的forAll与行为功能结合使用,但我遇到了问题。行为函数如下所示:

def someBehaviour(args: FunArgs)

其中FunArgs基本上是我要验证的各种可能性的联合类型。因此,有一个FunArgs1和一个FunArgs2都继承自FunArgs但有不同的成员(例如一个有两个Int而另一个有String },LongInt)。我想测试的实际功能在内部处理这些不同的情况。

sealed trait FunArgs
final case class FunArgs1(a: Int, b: Int) extends FunArgs
final case class FunArgs2(x: Long, y: Int, z: String) extends FunArgs

sealed trait FunRes
final case class FunRes1(...) extends FunRes
final case class FunRes2(...) extends FunRes

def someFun(args: FunArgs): FunRes = args match {
  // do stuff and return some subclass of FunRes
}

trait SomeBehaviors { this: UnitSpec =>

  def someBehavior(args: FunArgs) {

    it should "do stuff " in {
      val result = someFun(args) // creates an instance of FunRes
      ...
      result should contain theSameElementsAs expected
    }

    it should "do more stuff" in {
      // similar to before
      ...
      result should contain theSameElementsAs expected
    }
  }
}

行为函数内部有多个in子句,因为所有函数都需要满足相同的行为。

问题是我不能这样做,因为它会创建重复的测试名称:

forAll { (a: Int, b: Int) =>
  someBehavior(FunArgs1(a, b))
}

但我也不能这样做,因为'应该'内部'在':

"someFun1" should "do stuff" in {
  forAll { (a: Int, b: Int) =>
    someBehaviour(FunArgs1(a, b))
  }
}

下面这一行并没有给我使用生成器的可能性,因为我必须将forAll置于我不一定知道参数的数量和类型的行为中。< / p>

"someFun1" should behave like someBehaviour(MakeFunArgs1(a, b))

有没有办法做我想做的事?

1 个答案:

答案 0 :(得分:0)

你可以做这样的事情

public function getUserReg($observer){

    $session = Mage::getSingleton('core/session');
    $session_id = $session->getEncryptedSessionId();

    $customer_data = Mage::getSingleton('customer/session')->getCustomer();
    $user_id = $customer_data->entity_id;

    Mage::log("User Registration SESSION: " . $session_id, null, 't.log');
    Mage::log("User Registration USER: " . $user_id, null, 't.log');
}

您可以使用唯一的testCaseName调用以下内容:

trait SomeBehaviors { this: UnitSpec =>

  def someBehavior(index:Int, args: FunArgs) {

    it should s"do stuff $index" in {
      val result = someFun(args) // creates an instance of FunRes
      ...
      result should contain theSameElementsAs expected
    }

    it should "do more stuff $index" in {
      // similar to before
      ...
      result should contain theSameElementsAs expected
    }
  }
}

代替索引,您还可以使用&amp ;;生成唯一名称。 b,那将解决问题。