4

Spec2 を使用して、テスト クラスに定義済みのテスト メソッド チェーンがあります。

def is =
  "EntriesServlet with logged user" ^
  "POST / request should update entry that user owns" ! updateExistingEntryThatLoggedUserOwns ^
  "POST / request should not update non existing entry" ! notUpdateNonExistingEntry ^
  "POST / request should not update non owner entry" ! notAllowToUpdateNotOwnedEntry
end

これらのメソッドでは、定義されたモックが呼び出されたかどうかを確認しています。しかし、グローバルではなく 1 つのメソッドの呼び出しのみをカウントできるように、1 つのモックを再作成する必要があります。

だから私が必要としているのは、メソッドをシームレスに定義する方法です。

 def prepareMocks = {
   serviceMock = mock[MyService]
 }

これは各テスト メソッドの前に実行されるため、アサーションをチェックする前にクリーンなモックを準備できます。

特性BeforeEachBeforeExampleSpec2 を試しましたが、探しているものではありません。

4

1 に答える 1

2

ケース クラスを使用してモックをインスタンス化し、同時に実行される他の例からそれらを分離できます。

import org.specs2._
import specification._
import mock._

class MySpec extends Specification { def is =
  "EntriesServlet with logged user" ^
    "POST / request should update entry that user owns" !        c().updateExistingEntryThatLoggedUserOwns ^
    "POST / request should not update non existing entry" ! c().notUpdateNonExistingEntry ^
    "POST / request should not update non owner entry" ! c().notAllowToUpdateNotOwnedEntry ^
  end

  trait MyService
  case class c() extends Mockito {
    val service = mock[MyService]

    def updateExistingEntryThatLoggedUserOwns = service must not beNull
    def notUpdateNonExistingEntry = ok
    def notAllowToUpdateNotOwnedEntry = ok
  }
}

// here's a similar solution using standardised group names which is a 1.12.3 feature

class MySpec extends Specification { def is =
  "EntriesServlet with logged user" ^
    "POST / request should update entry that user owns"   ! g1().e1 ^
    "POST / request should not update non existing entry" ! g1().e2 ^
    "POST / request should not update non owner entry"    ! g1().e3 ^
  end

  trait MyService
  "POST requests" - new g1 with Mockito {
    val service = mock[MyService]

    e1 := { service must not beNull }
    e2 := ok
    e3 := ok
  }
}
于 2012-11-26T09:51:18.487 に答える