我有以下类要测试
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) }
}
}
Condition not satisfied:
notification.passivePlayer == passivePlayer
| | | |
| Baltar (null) | null
| false
Adama (null) foo to Baltar (null)
at bsg.NotificationServiceSpec.test create1(NotificationServiceSpec.groovy:44)
好吧,有趣的故事...我正在参加NFJS RWX/CDX会议,在发布这个问题后,我立即和彼得·尼德维瑟一起坐在电梯里。我们到底层的时候,他已经把我整理好了。
如果你停下来想一分钟,这是完全有意义的。我肯定会弄错细节,但我的基本理解是,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)
}
...
彼得说他会马上回答这个问题,我相信他会有更好的解释。希望他有时间;如果他这么做了,我会接受他的回答。
我正在将Grails2中的一系列单元测试升级到Grails3,并在使用Spock数据驱动测试格式驱动测试的域测试中遇到问题。 当我执行测试时,因为测试执行没有填充测试中的错误、字段和val引用。如前所述,这个测试适用于Grails2.5.5,所以我怀疑我缺少了Grails3中需要的一些东西。 Edited:我删除了原来在setup()中的mockForConstraints()调用,使其无效。
问题是,如果我使sqlService,Grails依赖项注入不起作用,它只创建一个空对象。如果我尝试将其设置为静态(如),情况也是如此。 我尝试将一个新的SqlService实例小型化,就像我的setupSpec块中所示: 这只是给出了一个错误 有人知道我如何在Spock测试中使用另一个服务类作为数据提供者吗?
在我的公司,我使用SeleniumWebDriver进行自动化测试来自动化Web应用程序,但我直接使用id、xpath和所有工具,而不遵循任何框架,因此现在我遇到了一些问题,必须设计数据驱动的框架。 请建议我如何在eclipse下设置DatadrivenFramwework。要遵循哪些步骤和所有步骤。 谢谢
编写测试代码时,一个较好的办法是把测试的输入数据和期望的结果写在一起组成一个数据表:表中的每条记录都是一个含有输入和期望值的完整测试用例,有时还可以结合像测试名字这样的额外信息来让测试输出更多的信息。 实际测试时简单迭代表中的每条记录,并执行必要的测试。这在练习 13.4 中有具体的应用。 可以抽象为下面的代码段: var tests = []struct{ // Test table
问题内容: 我正在尝试通过MSTest / Selenium在C#中进行数据驱动的测试。这是我尝试设置的一些代码示例: 这是我的错误:错误3非静态字段,方法或属性’Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DataRow.get’需要对象引用E:\ Projects \ SeleniumProject \ SeleniumPr
问题内容: 我是量角器的新手。谁能指导我使用量角器进行数据驱动的测试。下面是代码,配置文件和testdata.json文件。 配置文件: Json文件: 问题是它不是在获取数据,而是在所有输入框中写入未定义的内容。请帮忙 问题答案: 我假设它是一个 对象数组 ,您可以迭代每个数组元素并直接访问其内容,而您不需要的话,可以尝试如下操作: 我尚未测试以上代码,因此您应该使用 Page Objects,