scala 方法的单元测试,该方法调用其他方法

vfh0ocws  于 2023-05-17  发布在  Scala
关注(0)|答案(1)|浏览(138)

我有一个类A,它有私有方法ab以及公共方法c
测试涵盖了ab方法。
公共方法c是带参数的,因此它可以用if stmt调用ab
我想测试方法cbehavior-based test(测试a调用一些参数和b调用其他)。我不知道如何用ScalaTestScalaMock实现这一点
类代码的一部分

protected final def generate(leftBranch: List[L], leftPoint: L, rightBranch: List[L], rightPoint: L): C = {
    val lEmpty = leftBranch.isEmpty
    val rEmpty = rightBranch.isEmpty
    if (lEmpty && rEmpty) {
      val cc = leafOrdering.compare(leftPoint, rightPoint)
      if (cc < 0) {
        nextOrExpand(leftPoint){ p =>
          if (leafOrdering.compare(p, rightPoint) < 0) {
            usePoint(p)

          } else
            expand(leftPoint)

        }
      }
      else if (cc == 0)
        throw SamePointException()

      else
        throw LowerRestrictionException()

    }
    else if (lEmpty) {
      val cc = leafOrdering.compare(leftPoint, rightBranch.head)
      if (cc < 0)
        nextOrExpand(leftPoint){ p =>
          if (leafOrdering.compare(p, rightBranch.head) <= 0) {
            usePoint(p)

          } else {
            expand(leftPoint)
          }

        }

      else if (cc == 0)
        minimalRestricted(List(leftPoint), startBranchWith, rightBranch.tail, rightPoint)

      else
        throw LowerRestrictionException()

    }
    else if (rEmpty)
      if (leafOrdering.compare(leftBranch.head, rightPoint) < 0)
        next(leftBranch, leftPoint)

      else
        throw LowerRestrictionException()

    else {
      if (leafOrdering.compare(leftBranch.head, rightBranch.head) < 0)
        next(leftBranch, leftPoint)

      else {        
        throw LowerRestrictionException()

      }

    }
  }

方法nextOrExpandusePointexpandminimalRestrictednext是私有的并经过测试。generate方法是一个需要测试的公共方法。

f0ofjuux

f0ofjuux1#

如果我没有记错的话,使用behavior-based test,您希望像here示例那样编写测试

class SetSpec extends AnyWordSpec {

  // Describe a scope for a subject, in this case: "A Set"
  "A Set" can { // All tests within these curly braces are about "A Set"

    // Can describe nested scopes that "narrow" its outer scopes
    "empty" should { // All tests within these curly braces are about "A Set (when empty)"

      "have size 0" in {    // Here, 'it' refers to "A Set (when empty)". The full name
        assert(Set.empty.size == 0) // of this test is: "A Set (when empty) should have size 0"
      }
      "produce NoSuchElementException when head is invoked" in { // Define another test
        intercept[NoSuchElementException] {
          Set.empty.head
        }
      }
      "should be empty" ignore { // To ignore a test, change 'it' to 'ignore'...
        assert(Set.empty.isEmpty)
      }
    }

    // Describe a second nested scope that narrows "A Set" in a different way
    "non-empty" should { // All tests within these curly braces are about "A Set (when non-empty)"

      "have the correct size" in { // Here, 'it' refers to "A Set (when non-empty)". This test's full
        assert(Set(1, 2, 3).size == 3)     // name is: "A Set (when non-empty) should have the correct size"
      }
      // Define a pending test by using (pending) for the body
      "return a contained value when head is invoked" is (pending)
      import tagobjects.Slow
      "be non-empty" taggedAs (Slow) in { // Tag a test by placing a tag object after the test name
        assert(Set(1, 2, 3).nonEmpty)
      }
    }
  }
}

如果这是您想要的,您将能够看到您可以使用不同的风格mix different traits来编写测试。另外,根据您选择的是哪一个,测试的输出也会不同。
scalatest还提供了一个丰富的DSL,可以让您编写接近自然语言的测试。您可以使用提供不同方法的matchers,如havebecontains

相关问题