禁用全局Spock Mock类进行一次测试

问题描述 投票:0回答:1

您好我在安装方法中有一个全局模拟但是想在同一个类中为一个测试禁用它。可能吗?

class Test extends Specification {

  void setup() {
    GroovyMock(Utils, global: true)
    Utils.getRemoteBranches(*_) >> new ArrayList<String>()
  }

  void "test1"() {
    given:
    Object context = getContext()

    when:
    ...
java testing spock
1个回答
0
投票

你可以重命名你的setup方法,并在你的given步骤中调用它。 Spock中还有一个setup关键字。它与given具有相同的含义。

例如,像这样:

class Test extends Specification {

  void setupMock() {
    GroovyMock(Utils, global: true)
    Utils.getRemoteBranches(*_) >> new ArrayList<String>()
  }

  void "testWithMock"() {
    given:
    setupMock()
    Object context = getContext()

    when:
    // ...

  void "testWithoutMock"() {
    given:
    Object context = getContext()

    when:
    // ...

  }

在某种程度上我同意关于测试可读性的评论,没有Mock的测试相对重要,你应该考虑创建一个单独的qazxsw poi。

© www.soinside.com 2019 - 2024. All rights reserved.