3

最近、Grails1.3.7プロジェクトをGrails2.0.4にアップグレードしましたが、モックを使用した単体テストの多くが失敗し始めていることに気付きました。コントローラのテストは問題なく合格しているようです。問題は、サービスが相互にコラボレーションしていて、コラボレーターへの呼び出しをモックアウトしようとしたときに発生します。奇妙なことに、単一のテストを実行すると合格しますが、スイート全体を実行するとすぐに失敗し、エラーが発生します。

No more calls to 'getName' expected at this point. End of demands.
junit.framework.AssertionFailedError: No more calls to 'getName' expected at this point. End of demands.

新しいMockFor()の代わりにGMockを使用しようとしましたが、これと非常によく似たエラーが発生します。

No more calls to 'getSimpleName' expected at this point. End of demands.
junit.framework.AssertionFailedError: No more calls to 'getSimpleName' expected at this point. End of demands.

これは、私が得ているエラーを複製する方法を示す不自然な例と、https://github.com/punkisdead/FunWithMocksにあるGitHubのサンプルプロジェクト全体です。これを機能させる方法について何かアイデアはありますか?

BarController:

package funwithmocks

class BarController {

  def barService
  def fooService

  def index() { }
}

BarService:

package funwithmocks

class BarService {

  def fooService
  def bazService

  def serviceMethod() {

  }
}

BarControllerTests:

package funwithmocks

import grails.test.mixin.*
import org.junit.*
import groovy.mock.interceptor.MockFor

/**
 * See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
 */
@TestFor(BarController)
class BarControllerTests {

  def fooService
  def barService

  @Before
  public void setUp() {
    fooService = new MockFor(FooService)
    fooService.use {
      controller.fooService = new FooService()
    }

    barService = new MockFor(BarService)
    barService.use {
      controller.barService = new BarService()
    }
  }

  @Test
  void doSomething() {
    controller.index()
  }
}

BarServiceTests:パッケージfunwithmocks

import grails.test.mixin.*
import org.junit.*
import groovy.mock.interceptor.MockFor

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(BarService)
class BarServiceTests {

  def fooService
  def bazService

  @Before
  public void setUp() {
    fooService = new MockFor(FooService)
    fooService.use {
      service.fooService = new FooService()
    }

    bazService = new MockFor(BazService)
    bazService.use {
      service.bazService = new BazService()
    }
  }

  @Test
  void callSomeService() {
    service.serviceMethod()
  }
}
4

1 に答える 1

1

MockFor新しいテスト mixin をgroovy クラスと組み合わせるべきではありません。MockForすべてのインスタンスをmockForメソッドに置き換えます。

http://grails.org/doc/latest/guide/testing.html#mockingCollaborators

于 2012-09-06T14:51:00.860 に答える