gpt4 book ai didi

grails - 数据驱动的Spock测试

转载 作者:行者123 更新时间:2023-12-02 15:30:23 24 4
gpt4 key购买 nike

我有以下类(class)要测试

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。有人可以帮我了解发生了什么吗?

最佳答案

好吧,有趣的故事...
我正在NFJS RWX / CDX session 上,发布此问题后,我立即与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)
}
...

彼得说,他会稍后再回答,我相信他会提供更好的解释。希望他有时间。如果他同意,我会接受他的回答。

关于grails - 数据驱动的Spock测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20412646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com