数据驱动的Spock测试

时间:2013-12-05 23:04:48

标签: grails groovy spock

我有以下要测试的课程

package bsg

class NotificationService {

    def create(String publicMsg, String privateMsg = null, Player activePlayer, Player passivePlayer = null) {
        new Notification(
                game: activePlayer.game, publicMessage: publicMsg, privateMessage: privateMsg,
                activePlayer: activePlayer, passivePlayer: passivePlayer
                ).save(failOnError: true)
    }

}

以下测试类

package bsg

import grails.test.mixin.*
import spock.lang.Specification
import spock.lang.Unroll

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(NotificationService)
@Mock([Notification, Game, Player])
class NotificationServiceSpec extends Specification {

    NotificationService service

    private static final String HIDDEN_SIDE = "1 - Launch Scout"
    private static Player adama
    private static Player baltar
    private static Game game
    private static Card card
    private static NotificationService notificationService

    def setup() {
        service = new NotificationService()
        game = new Game(name: "foo").save(validate:false)
        card = new Card(hiddenSide: HIDDEN_SIDE, backSide: "Bacon", deck: new Deck(backSide: "Skill Card"))
        adama = new Player(game: game, character:Player.Character.Adama).save(validate: false)
        baltar = new Player(game: game, character:Player.Character.Baltar).save(validate: false)
        notificationService = service
    }

    @Unroll
    def "test create1"(String privateMsg, Player passivePlayer, Closure serviceMethod) {
        when: "I create a notification"
        Notification notification = serviceMethod.call()

        then: "a notification has been persisted"
        notification?.id != null

        and: "it displays the correct messages"
        notification.publicMessage == "foo"
        notification.privateMessage == privateMsg
        notification.activePlayer == adama
        notification.passivePlayer == passivePlayer

        where:
        privateMsg  | passivePlayer | serviceMethod
        "bar"       | baltar        | { notificationService.create("foo", "bar", adama, baltar) }
        null        | baltar        | { notificationService.create("foo", null, adama, baltar) }
        "bar"       | null          | { notificationService.create("foo", "bar", adama) }
        null        | null          | { notificationService.create("foo", adama) }
    }

    @Unroll
    def "test create2"(String privateMsg, Player passivePlayer, Closure serviceMethod) {
        when: "I create a notification"
        Notification notification = serviceMethod.call()

        then: "a notification has been persisted"
        notification?.id != null

        and: "it displays the correct messages"
        notification.publicMessage == "foo"
        notification.privateMessage == privateMsg
        notification.activePlayer == adama
        notification.passivePlayer == passivePlayer

        where:
        privateMsg  | passivePlayer | serviceMethod
        "bar"       | baltar        | { notificationService.create("foo", "bar", adama, baltar) }
        null        | baltar        | { notificationService.create("foo", null, adama, baltar) }
        "bar"       | null          | { notificationService.create("foo", "bar", adama) }
        null        | null          | { notificationService.create("foo", adama) }
    }
}

两个测试都是相同的,但在"test create1"()中,前两个测试失败,后两个测试失败。在"test create2"()中,所有4个测试都通过。两个失败的错误消息是:

Condition not satisfied:

notification.passivePlayer == passivePlayer
|            |             |  |
|            Baltar (null) |  null
|                          false
Adama (null) foo to Baltar (null)

at bsg.NotificationServiceSpec.test create1(NotificationServiceSpec.groovy:44)

所以看起来静态类变量baltar在某种程度上没有为第一次测试设置。有人可以帮我理解发生了什么吗?

1 个答案:

答案 0 :(得分:1)

好吧,有趣的故事...... 我正在参加NFJS RWX / CDX会议,在发布这个问题之后我立即和Peter Niederwieser一起上了电梯。当我们到达底层时,他让我整理好了。

如果你停下来思考一分钟,这是完全合理的。我确信我会得到错误的细节,但我的基本理解是Spock在执行setup()方法之前从数据表构建实际的junit测试方法。值得庆幸的是,使用setupSpec()将完成我正在寻找的目标。

所以这就是我所做的修复它。谢谢,彼得。

...
private static final String HIDDEN_SIDE = "1 - Launch Scout"
private static Player adama
private static Player baltar
private static Game game
private static Card card
private static NotificationService notificationService

def setup() {
    notificationService = new NotificationService()

    game.save(validate:false)
    adama.save(validate: false)
    baltar.save(validate: false)
}

def setupSpec() {
    game = new Game(name: "foo")
    card = new Card(hiddenSide: HIDDEN_SIDE, backSide: "Bacon", deck: new Deck(backSide: "Skill Card"))
    adama = new Player(game: game, character:Player.Character.Adama)
    baltar = new Player(game: game, character:Player.Character.Baltar)
}
...
彼得说他会跳起来稍后回答,我相信他会有更好的解释。希望他有时间;如果他这样做,我会接受他的回答。