0

機能テストの開発 1 つのステップの結果を次のステップの入力として使用して、ワークフローをシミュレートする必要があります。例は次のとおりです。

  1. 指定された条件でホテル\部屋を検索
    • リクエストが成功したことを確認する
    • 少なくともいくつかの結果があることを確認してください
  2. ステップ 1 からランダムに部屋を選択します。
  3. ステップ2からお部屋をご予約ください。
    • リクエストが成功したことを確認する
  4. ステップ3から予約をキャンセルします。
    • リクエストが成功したことを確認する

ここでの重要なポイントは次のとおりです。

  • 1をせずに3を実行することはできません。
  • 3.をしなければ4.はできません。
  • ステップが失敗した場合は、機能を中止する必要があります

そのような場合の仕様を開発するためのアプローチは何ですか?

4

1 に答える 1

2

sequential最も簡単な方法は、プロセスと仕様を表す変更可能なオブジェクトを用意することです。

class HotelSpec extends mutable.Specification { sequential
  val hotel = new HotelProcess

  "get a room available on Monday" >> ifHotelOk {
    val rooms = request(MONDAY)
    hotel.selectedRooms = rooms
    rooms must not beEmpty
  }

  "book the room" >> ifHotelOk {
    val booking = bookRoom(hotel.selectedRooms.head)
    hotel.currentBooking = booking
    booking must beOk
  }

  def ifHotelOk(r: =>Any) = if (hotel.canContinueProcess) {
    try { r; hotel.continueProcess }
    catch { case t: Throwable => hotel.stopProcess; throw t }
  } else skipped("hotel process error in previous steps")
}

[アップデート]

var がより適切にカプセル化されている別の方法を次に示します。

import org.specs2._
import org.specs2.execute._
import org.specs2.specification.FixtureExample

class HotelSpec extends HotelProcessSpec {
  "get a room available on Monday" >> { hotel: HP =>
    val rooms = request(MONDAY)
    rooms must be empty

    // update the state of the process at the end of the example
    hotel.selectedRoomsAre(rooms)
  }

  // this example will only execute if the previous step was ok
  "book the room" >> { hotel: HP =>
    val booking = bookRoom(hotel.selectedRooms.head)
    booking.booked must beTrue
  }

  val MONDAY = "monday"
  def request(day: String): Seq[Room] = Seq(Room())
  def bookRoom(room: Room) = Booking()
}

/**
 * A specification trait encapsulating the process of booking hotel rooms
 */
trait HotelProcessSpec extends mutable.Specification with FixtureExample[HotelProcess] {
  sequential

  type HP = HotelProcess
  private var hotelProcess = HotelProcess()

  // if the hotelProcess is returned as the last statement of an Example
  // set the new value of the hotelProcess and return Success
  implicit def hotelProcessAsResult: AsResult[HotelProcess] = new AsResult[HotelProcess] {
    def asResult(hp: =>HotelProcess) =
      try { hotelProcess = hp; Success() }
      catch { case t: Throwable => hotelProcess = hotelProcess.stop; throw t }
  }

  /**
   * stop executing examples if one previous step failed
   */
  protected def fixture[R : AsResult](f: HotelProcess => R): Result = {
    if (hotelProcess.continue) {
      val result = AsResult(f(hotelProcess))
      if (!result.isSuccess) hotelProcess = hotelProcess.stop
      result
    }
    else                       skipped(" - SKIPPED: can't execute this step")
  }

}

case class HotelProcess(selectedRooms: Seq[Room] = Seq(), continue: Boolean = true) {
  def stop = copy(continue = false)
  def selectedRoomsAre(rooms: Seq[Room]) = copy(selectedRooms = rooms)
}
case class Room(number: Int = 0)
case class Booking(booked: Boolean = true)
于 2013-09-09T21:17:34.997 に答える