首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据驱动的Spock测试

数据驱动的Spock测试
EN

Stack Overflow用户
提问于 2013-12-05 23:04:48
回答 1查看 693关注 0票数 0

我有下面的课程要测试

代码语言:javascript
复制
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)
    }

}

和下面的测试类

代码语言:javascript
复制
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个测试都通过了。这两种故障的错误消息如下:

代码语言:javascript
复制
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似乎没有为第一个测试设置。有人能帮我了解一下发生了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-06 00:45:11

有趣的故事..。我正在NFJS RWX/CDX会议上,在发布这个问题后,我立刻和一起在电梯里得到了这个问题。我们到底层的时候他就把我安排好了。

如果你停下来想一想,这是很有道理的。我肯定我会弄错细节,但我的基本理解是,史波克在执行setup()方法之前从数据表构建实际的junit测试方法。谢天谢地,使用setupSpec()可以完成我想要的任务。

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

代码语言:javascript
复制
...
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)
}
...

彼得说他会跳过去回答这个问题,我相信他会有一个更好的解释。希望他有时间,如果他有时间的话,我会接受他的回答。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20412646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档